0

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"

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
MattFiler
  • 175
  • 2
  • 15

2 Answers2

1

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 {
}
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
1

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.

vrwim
  • 13,020
  • 13
  • 63
  • 118
  • Helpful, thanks. I don't know why they have to update the language, it's annoying for learners like me. – MattFiler Jun 09 '15 at 21:05
  • @MattFiler Don't forget to accept the answer if it solved your problem and upvote if it exceeded your expectations – vrwim Jun 09 '15 at 21:06
  • Have done, thank you! I'm also using a UIWebView in my project and I'm now getting the error of "NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)" after the Xcode update. Do you know what could be causing this? It worked absolutely fine before but now it won't load my webpages. – MattFiler Jun 09 '15 at 21:21
  • @MattFiler nope, don't know. Seems to be something SSL-related. Search the error domain (`kCFStreamErrorDomainSSL`) and error code (`9802`). Make a new question if you can't find it. – vrwim Jun 09 '15 at 21:24
  • Thanks. After searching it seems a few people are having the same problem, this one fixed it for me: http://stackoverflow.com/questions/30720813/cfnetwork-sslhandshake-failed-ios-9-beta-1 – MattFiler Jun 09 '15 at 21:40