文件操作

沙盒路径

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)

混饭小能手
36 声望2 粉丝

前端开发者