我试图设置属性NSURLIsExcludedFromBackupKey
,但我找不到文件夹(或文件,如果我尝试单独对每个文件执行此操作),则找不到。
代码如下:
override func viewDidLoad() {
var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
var documentsDirectory = paths[0] as? String
documentsRoot = documentsDirectory! + "/"
var root = NSURL(string: documentsRoot!)
addSkipBackupAttributeToItemAtURL(root!)
}
func addSkipBackupAttributeToItemAtURL(URL: NSURL) -> Void {
var filepath = NSURL(fileURLWithPath: "\(URL)")
let fileManager = NSFileManager.defaultManager()
assert(fileManager.fileExistsAtPath(URL.absoluteString))
var error:NSError?
do {
try filepath.setResourceValue(NSNumber(bool: true), forKey: NSURLIsExcludedFromBackupKey)
}
catch let error as NSError {
print("Error excluding \(filepath.lastPathComponent) from backup \(error)")
}
return
}
当我运行代码时,我得到了这个输出
从备份错误中排除Optional(" ... cuments")时出错 Domain = NSCocoaErrorDomain Code = 4"文件“... cuments”没有 存在&#34。 UserInfo = {NSURL = file:///Users/me/Library/Developer/CoreSimulator/Devices/D1FC9BFB-4F95-440D-A3C5-ED1C0665A610/data/Containers/Data/Application/%20...%20cuments/,NSFilePath = /用户/我/库/开发商/ CoreSimulator /设备/ D1FC9BFB-4F95-440D-A3C5-ED1C0665A610 /数据/集装箱/数据/应用/ ... cuments,NSUnderlyingError = 0x7f86b2c80850 {错误 Domain = NSPOSIXErrorDomain Code = 2"没有这样的文件或目录"}}
我做错了什么?