Swift: optional error argument in call

I am currently developing my first iOS application using Swift 2.0 and Xcode Beta 2. It reads external JSON and generates a list in the form of a table with data. However, I get a strange little error that I cannot fix:

Extra argument 'error' in call 

Here is a snippet of my code:

 let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in print("Task completed") if(error != nil){ print(error!.localizedDescription) } var err: NSError? if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary{ if(err != nil){ print("JSON Error \(err!.localizedDescription)") } if let results: NSArray = jsonResult["results"] as? NSArray{ dispatch_async(dispatch_get_main_queue(), { self.tableData = results self.appsTableView!.reloadData() }) } } }) 

The error is displayed on this line:

 if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary{ 

Can someone please tell me what I'm doing wrong here?

+46
ios swift2 xcode7-beta2
Jun 26 '15 at 12:43 on
source share
3 answers

With Swift 2, the signature for NSJSONSerialization has changed to fit the new error handling system.

Here is an example of how to use it:

 do { if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary { print(jsonResult) } } catch let error as NSError { print(error.localizedDescription) } 

With Swift 3, the NSJSONSerialization name and its methods have changed in accordance with the Swift API Design Guide .

Here is the same example:

 do { if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] { print(jsonResult) } } catch let error as NSError { print(error.localizedDescription) } 
+75
Jun 26 '15 at 12:58
source share

Changes in Swift 2 have changed; methods that accept the error parameter have been converted to methods that generate this error instead of returning it using the inout . By looking at the Apple documentation :

ERROR REPORTING IN SWIFT: In Swift, this method returns a non-optical result and is marked with the throw keyword to indicate that it throws an error in the event of a failure.

You call this method in a try statement and handle any errors in the catch clauses of the do statement, as described in Handling Errors in Swift Programming (Swift 2.1) and Handling Errors When Using Swift with Cocoa and Objective-C (Swift 2.1).

The shortest solution is to use try? which returns nil if an error occurs:

 let message = try? NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments) if let dict = message as? NSDictionary { // ... process the data } 

If you are also interested in the error, you can use do/catch :

 do { let message = try NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments) if let dict = message as? NSDictionary { // ... process the data } } catch let error as NSError { print("An error occurred: \(error)") } 
+5
Jan 10 '16 at 10:11
source share

This has been changed in Swift 3.0.

  do{ if let responseObj = try JSONSerialization.jsonObject(with: results, options: .allowFragments) as? NSDictionary{ if JSONSerialization.isValidJSONObject(responseObj){ //Do your stuff here } else{ //Handle error } } else{ //Do your stuff here } } catch let error as NSError { print("An error occurred: \(error)") } 
0
Sep 29 '16 at 9:44
source share



All Articles