1

I am New for Swift and I Have Implement File Manager Concept in my Project but it shows the issue and I don't for how to solve this please any body help me for fix the issue.

Here I Post My Code.

class func addSkipBackupAttributeToItemAtPath(filePathString: String) throws -> Bool
{
    let URL: NSURL = NSURL.fileURLWithPath(filePathString)
    assert(NSFileManager.defaultManager().fileExistsAtPath(URL.path!))
    let err: NSError? = nil

    let success: Bool = try URL.setResourceValue(Int(true), forKey: NSURLIsExcludedFromBackupKey) //---- This Line Shows the Issue.

    if !success {
        NSLog("Error excluding %@ from backup %@", URL.lastPathComponent!, err!)
    }
    return success
}
Saravana Kumar B
  • 215
  • 1
  • 2
  • 6

2 Answers2

2

The benefit of the new error handling in Swift 2 is the omission of the quasi-redundant return values Bool / NSError. Therefore setResourceValue does not return a Bool anymore which is the reason of the error message.

As the function is marked as throws I recommend this syntax which just passes the result of setResourceValue

class func addSkipBackupAttributeToItemAtPath(filePathString: String) throws
{
    let url = NSURL.fileURLWithPath(filePathString)
    assert(NSFileManager.defaultManager().fileExistsAtPath(URL.path!))

    try url.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
}

Handle the error in the method which calls addSkipBackupAttributeToItemAtPath

vadian
  • 232,468
  • 27
  • 273
  • 287
1

The method setResourceValue is a throw function and does not return a Bool.

Try running your function using a do-catch:

do {
    try URL.setResourceValue(Int(true), forKey: NSURLIsExcludedFromBackupKey)
}
catch {
    NSLog("Error excluding %@ from backup %@", URL.lastPathComponent!, err!)
}
Julian Lee
  • 293
  • 1
  • 11