After updated to Swift 2.0, when NSFielManager
is called, it has caused the following error. Could you tell me what is the problem?
let cachesDirectoryURL = NSFileManager().URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
Error:
"Call can throw, but errors cannot be thrown out of a property initializer"
If you are declaring this as global in a class you need to add prefix "try!" to value you are assigning.
like this
let cachesDirectoryURL = try! NSFileManager().URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
Which means we have to catch the error that might be thrown, should problem occurs:
do
{
let cachesDirectoryURL = try NSFileManager().URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
}
catch let error as NSError
{
print(error.localizedDescription)
}
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