iOS应用程序只能在自己的目录下进行文件的操作,不可以访问其他的存储空间,此区域被称为沙盒。下面介绍常用的程序文件夹目录:
|
1
2
|
//获取程序的Home目录let homeDirectory = NSHomeDirectory() |
|
1
2
3
4
5
6
7
|
//方法1let documentPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)let documnetPath = documentPaths[0] as! String//方法2let ducumentPath2 = NSHomeDirectory() + "/Documents" |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//Library目录-方法1let libraryPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)let libraryPath = libraryPaths[0] as! String//Library目录-方法2let libraryPath2 = NSHomeDirectory() + "/Library"//Cache目录-方法1let cachePaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)let cachePath = cachePaths[0] as! String//Cache目录-方法2let cachePath2 = NSHomeDirectory() + "/Library/Caches" |
|
1
2
3
4
5
|
//方法1let tmpDir = NSTemporaryDirectory()//方法2let tmpDir2 = NSHomeDirectory() + "/tmp" |
5,程序打包安装的目录 NSBundle.mainBundle()
工程打包安装后会在NSBundle.mainBundle()路径下,该路径是只读的,不允许修改。
所以当我们工程中有一个SQLite数据库要使用,在程序启动时,我们可以把该路径下的数据库拷贝一份到Documents路径下,以后整个工程都将操作Documents路径下的数据库。
|
1
2
3
4
5
6
7
8
9
|
//声明一个Documents下的路径var dbPath = NSHomeDirectory() = "/Documents/hanggeDB.sqlite"//判断数据库文件是否存在if !NSFileManager.defaultManager().fileExistsAtPath(dbPath){ //获取安装包内数据库路径 var bundleDBPath:String? = NSBundle.mainBundle().pathForResource("hanggeDB", ofType: "sqlite") //将安装包内数据库拷贝到Documents目录下 NSFileManager.defaultManager().copyItemAtPath(bundleDBPath!, toPath: dbPath, error: nil)} |
Swift - 常用文件目录路径获取(Home目录,文档目录,缓存目录等)
原文:http://www.cnblogs.com/Free-Thinker/p/4843354.html