无法设置 NSURLIsExcludedFromBackupKey 属性
Cannot set NSURLIsExcludedFromBackupKey property
我正在尝试设置属性 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
}
当我 运行 我得到这个输出的代码时
Error excluding Optional(" ... cuments") from backup Error
Domain=NSCocoaErrorDomain Code=4 "The file “ ... cuments” doesn’t
exist."
UserInfo={NSURL=file:///Users/me/Library/Developer/CoreSimulator/Devices/D1FC9BFB-4F95-440D-A3C5-ED1C0665A610/data/Containers/Data/Application/%20...%20cuments/, NSFilePath=/Users/me/Library/Developer/CoreSimulator/Devices/D1FC9BFB-4F95-440D-A3C5-ED1C0665A610/data/Containers/Data/Application/
... cuments, NSUnderlyingError=0x7f86b2c80850 {Error
Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
我做错了什么?
您似乎在此处使用 URL 的文字描述:
var filepath = NSURL(fileURLWithPath: "\(URL)") // \(URL) is a string that contains not just the path, but also the optionality of the variable
而您应该在 NSURL.
上使用 path 属性
我正在尝试设置属性 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
}
当我 运行 我得到这个输出的代码时
Error excluding Optional(" ... cuments") from backup Error Domain=NSCocoaErrorDomain Code=4 "The file “ ... cuments” doesn’t exist." UserInfo={NSURL=file:///Users/me/Library/Developer/CoreSimulator/Devices/D1FC9BFB-4F95-440D-A3C5-ED1C0665A610/data/Containers/Data/Application/%20...%20cuments/, NSFilePath=/Users/me/Library/Developer/CoreSimulator/Devices/D1FC9BFB-4F95-440D-A3C5-ED1C0665A610/data/Containers/Data/Application/ ... cuments, NSUnderlyingError=0x7f86b2c80850 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
我做错了什么?
您似乎在此处使用 URL 的文字描述:
var filepath = NSURL(fileURLWithPath: "\(URL)") // \(URL) is a string that contains not just the path, but also the optionality of the variable
而您应该在 NSURL.
上使用 path 属性