I'm trying to set attribute NSURLIsExcludedFromBackupKey
but I get exception of folder(or file, if I try to do this with each file separately) is not found.
code is following:
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
}
when I run the code I get this output
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"}}
What Im I doing wrong ?
It looks like you are using the text description of the URL here:
var filepath = NSURL(fileURLWithPath: "\(URL)") // \(URL) is a string that contains not just the path, but also the optionality of the variable
whereas you should use the path property on NSURL.