I'm trying to figure out how i should proceed fox fixing below code using Swift 3.
let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true) as NSArray
for path in paths {
let dir = path as! String
print("the paths are \(path)")
let urlToExclude = NSURL.fileURL(withPath: dir)
do {
try urlToExclude.setResourceValue(NSNumber(value: true), forKey: URLResourceKey.isExcludedFromBackupKey)
} catch { print("failed to set resource value") }
}
The error that i'm getting is
I used the above code for excluding files from backing up to the iCloud and it used to work fine for previous version of Swift but after updating to Xcode 8 I'm just stuck.
I would really appreciate any help or suggestions.
The Xcode suggestion "Use struct URLResourceValues and URL.setResourceValues(_:) instead" is prompting you to write something like this:
let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)
for dir in paths {
print("the paths are \(dir)")
var urlToExclude = URL(fileURLWithPath: dir)
do {
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try urlToExclude.setResourceValues(resourceValues)
} catch { print("failed to set resource value") }
}
Please try.
NOTE
Please note that the file URL has to be a var - if it's a let you will get strange error messages (see comments).
Additionally to what TheEye provided, make sure your URL is a var and not a let.
That is what made my code work after migrating to swift 3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With