“调用中的额外参数”我有这个错误....这段代码有什么问题

时间:2015-11-02 15:05:52

标签: ios database swift2

这段代码有什么问题?我试图从我的SQL数据库中检索数据。

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()
    }
}

1 个答案:

答案 0 :(得分:1)

实际上Swift2 NSError

上没有NSJSONSerialization

您需要将其包装在do/catch块中,因为这是报告错误的首选方式,而不是使用NSError

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

如果您需要NSError对象属性,请使用:

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)")
}