Xcode 7.0 Swift Update Problems

1.1k Views Asked by At

I'm trying to update my project to work with Xcode 7.0 and after updating my Swift projects I'm getting an error that I don't understand on this line.

let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary

The error is

"Call can throw, but it is not marked with 'try' and the error is not handled"

I'm also getting these two errors in my project files...

"linker command failed with exit code 1 (use -v to see invocation)"

and

"error: cannot parse the debug map for "/Users/MattFiler/Library/Developer/Xcode/DerivedData/ePlanner-cqwzlxqgpwaloubjgnzdlomjkfea/Build/Intermediates/SwiftMigration/ePlanner/Products/Debug-iphonesimulator/ePlannerTests.xctest/ePlannerTests": No such file or directory"

2

There are 2 best solutions below

5
On BEST ANSWER

Try this code:

do {
    let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options: .MutableContainers ) as! NSDictionary
    // Use jsonData here
} catch {
    print("Well something happened: \(error)")
}

You'll need the try keyword as NSJSONSerialization.JSONObjectWithData now throws an error if something failed since Swift 2. Throwing functions need to be marked with try or try!.

Also you'll need the do { ... } catch to catch any errors that may occur. This will catch the error and handle it.

You might want to read up on the changes in Swift 2 to understand why this happened. Also the WWDC videos will be very helpful.

2
On

You need to try and catch if it throws an error.

do {
    let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
    //...
}
catch {
}