1

What is wrong with this code? I am trying to retrieve data from my SQL database.

import Foundation

class Service {
    var settings:Settings!

    init(){
        self.settings = Settings()
    }

    func getContacts (callback:(NSDictionary)-> ()){
        request(settings.viewContacts, callback: callback)
    }

    func request(url: String , callback:(NSDictionary) ->()) {
        let nsURL = NSURL(string : url)

        let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL!){
            (data , reponse , error) in
            let error: NSError?

            var reponse = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers , error) as NSDictionary // extra argument in call , i am having this error.
            callback(reponse)
        }
        task.resume()
    }
}
Siyual
  • 16,415
  • 8
  • 44
  • 58
Zain Habib
  • 13
  • 5

1 Answers1

1

actually Swift2 has no NSError on NSJSONSerialization

You need to wrap it in a do/catch block as this is the preferred way of reporting errors, rather than using NSError:

do {
   let reponse = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
   // use reponse
    callback(reponse)
} catch {
    // report error
}

if you need the NSError object properties,use:

do {
    let reponse = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary

    // use reponse
    callback(reponse)
} catch let error as NSError {
    print("json error: \(error.localizedDescription)")
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • thanks brother i was trying this from last few days n you solved it :) – Zain Habib Nov 02 '15 at 16:14
  • @ZainHabib -- have a nice day my bro, actually swift 2.0 is readuced the code format, – Anbu.Karthik Nov 02 '15 at 16:15
  • brother i want to make an app which will retrieve contacts from database , so can you give me any hint how can i categories those contacts ?e.g all schools numbers , hospitals numbers , hotels etc ... do i have to make different tables in data base or there is some other way in swift ? – Zain Habib Nov 02 '15 at 16:21