这段代码有什么问题?我试图从我的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()
}
}
答案 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)")
}