swift笔记--获取沙箱中几个常用目录的路径

//获取沙箱结构中几个常见的目录
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
//        首先获应用程序目录的路径,该目录下有三个文件夹:文档目录,库目录,临时目录以及一个程序包。该目录就是应用程序沙盒,应用程序只能访问该目录下的内容
        let homePath = NSHomeDirectory()
        print("homePath:\(homePath)\n")
        
//        系统会为每个程序生成一个私有目录,并随机生成一个数字字母串作为目录名,在每次程序启动时这个名称都是不同的,而使用此方法,则可以获得对应的目录集合
        let documentPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
//        获得并输出文档目录,应该将所有的数据文件写入到该目录下,这个目录通常用来保存用户数据
        print("documentPath\(documentPath[0])")
        
//        创建一个字符串对象,该字符串对象同样标示沙箱中的文档目录
        let documentPath2 = NSHomeDirectory() + "/Documents"
        print("documentPath2:\(documentPath2)")
        
//        使用相同方式获得库目录,该目录下包括两个字目录,缓存目录和参数目录
        let libraryPath1 = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        print("libraryPaths:\(libraryPath1)")
//        创建一个字符串,同样标示沙箱中的库目录
        let libraryPath2 = NSHomeDirectory() + "/Library"
        print("libraryPaths2:\(libraryPath2)")
        
//        获得沙箱下的缓存目录
        let cachePaths1 = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
//        该目录用于存放应用程序专用的支持文件,保存应用程序再次启动过程中所需要的信息
        print("cachePaths1:\(cachePaths1)")
//        创建一个字符串对象,同样标示沙箱中的缓存目录
        let cachePaths2 = NSHomeDirectory() + "/Library/Caches"
        print("cachePaths2:\(cachePaths2)")
        
//        创建一个常量,用来储存当前用户的临时路径
        let tmpPath1 = NSHomeDirectory() + "/Tmp"
        print(tmpPath1)
        let tmpPath2 = NSTemporaryDirectory()
        print(tmpPath2)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_41735943/article/details/81218062
今日推荐