47

I'm currently developing my first iOS app using Swift 2.0 and Xcode Beta 2. It reads an external JSON and generates a list in a table view with the data. However, I'm getting a strange little error that I can't seem to 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 thrown at 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?

Yansin
  • 25
  • 4
kaanmijo
  • 713
  • 1
  • 5
  • 14
  • This line doesn't giving this error i think may b you are getting in different line. – Dharmbir Singh Jun 26 '15 at 12:57
  • With 2.0 you need to create a `do` `catch` block. `error` is no longer a parameter with `NSJSONSerialization`. There are a number of other answers to this question here. Look for do, catch with Swift 2.0 – Race B Jun 26 '15 at 12:57

3 Answers3

75

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

Here's 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 name of NSJSONSerialization and its methods have changed, according to the Swift API Design Guidelines.

Here's 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)
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • I am using swift 2.2, and tried your first version. It is compiling correctly but it is dying silently, does not hit either of the the `print` lines. When I remove the `if` part in front of `jsonResult` and run I can infer that the jsonResult is being set as `nil` no errors. I am sure the data starts as a valid json string... – jeffery_the_wind Jun 25 '16 at 07:03
  • 1
    @jeffery_the_wind Your JSON is probably different from what you think. Try this: http://stackoverflow.com/a/33510776/2227743 – Eric Aya Jun 25 '16 at 09:37
  • 1
    Thanks, yes that was it! Replacing `as? NSDictionary` with `as? NSArray` in your first example works for me. – jeffery_the_wind Jun 26 '16 at 12:50
5

Things have changed in Swift 2, methods that accepted an error parameter were transformed into methods that throw that error instead of returning it via an inout parameter. By looking at the Apple documentation:

HANDLING ERRORS IN SWIFT: In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure.

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

The shortest solution would be 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're also interested into the error, you can use a 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)")
}
Cristik
  • 30,989
  • 25
  • 91
  • 127
0

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)") }
CodeSteger
  • 107
  • 1
  • 3