文件操作
沙盒门路
NSHomeDirectory()
tmp
长期门路,随时会被删除 Documents
会备份,不容许存储缓存信息
Library
caches
缓存文件 Preferences
偏好设置,能够用来保留账号
获取文件信息
let cachesDictionary = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
let dist = cachesDictionary.appendingPathComponent("a/b/c.txt")
let fileManager = FileManager.default
do {let attributes = try fileManager.attributesOfItem(atPath: dist)
let fileSize = attributes[.size] as! UInt64 // 获取文件大小
print("File size: \(fileSize) bytes")
} catch {print(error)
}
获取某个文件的所有子门路
let subPaths = fileManager.subpaths(atPath: "path")
判断是否是文件夹
var isDirectory: ObjCBool = false
let isExist = fileManager.fileExists(atPath: "path", isDirectory: &isDirectory)