-2
class Get {

class func JSON(url:String,callback:@escaping (NSDictionary)->()) {
    requestJSON(url: url,callback: callback)
}

class func requestJSON(url:String,callback:@escaping (NSDictionary)->()) {
    var nsURL = NSURL(string: url)
    let task = URLSession.shared.dataTask(with: nsURL as! URL) {
        (data,response,error) in
        var error:NSError?

        var response = JSONSerialization.JSONObjectWithData(data, options: JSONSerialization.ReadingOptions.MutableContainers, error: &error) as NSDictionary
        callback(response)
    }
    task.resume()
}
}

getting error in this line "var response = JSONSerialization.JSONObjectWithData(data, options: JSONSerialization.ReadingOptions.MutableContainers, error: &error) as NSDictionary" as extra argument 'error' in call

please help me in sorting this error or please suggest me link for grabing JSON library.thanks in advance.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Sri Vathsav
  • 91
  • 1
  • 1
  • 3
  • Try removing 'error: &error' from your response call. – dylanthelion Mar 04 '17 at 07:30
  • @dylanthelion thanks for replying when i removed that i got this error "call can throw but it is not marked with 'try' and error is not handled " – Sri Vathsav Mar 04 '17 at 07:53
  • That means that JSONObjectWithData: throws, and needs to be put in a try block. I think you need to find a more recent example of the method you're using. JSON serialization was updated with Swift 3. You're using an implementation that worked a couple of years ago, but has essentially been deprecated, – dylanthelion Mar 04 '17 at 07:56
  • @dylanthelion sir can u please help me in sorting this problem i have to show the final output of our project by tomato.tanks in advance. – Sri Vathsav Mar 04 '17 at 08:46
  • I am unfortunately too busy to give you a direct answer, but Googling 'swift jsonobjectwithdata' and filtering to the last month, gave me this: https://www.raywenderlich.com/150322/swift-json-tutorial-2 and this: http://stackoverflow.com/questions/42242535/objective-c-to-swift-nsdata-with-nsjsonserialisation . Try these, and if they don't work, try filtering your searches for the past month. You're using an old method, and simply need an update. – dylanthelion Mar 04 '17 at 08:58
  • @dylanthelion tank so much sir ill try this – Sri Vathsav Mar 04 '17 at 09:01

1 Answers1

0

You're supposed to enclose the method in a try...catch block.

Just replace these two lines

var response = JSONSerialization.JSONObjectWithData(data, options: JSONSerialization.ReadingOptions.MutableContainers, error: &error) as NSDictionary
        callback(response)

with

do{
    let jsonObject = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
    if let response = jsonObject as? NSDictionary{
        callback(response)
    }
}
catch{
    print(error)
}

You can also do this instead

if let response = (try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)) as? NSDictionary{
    callback(response)
}
ebby94
  • 3,131
  • 2
  • 23
  • 31