使用NSURLConnection调用时额外的参数'error'

时间:2016-05-25 22:41:34

标签: json swift2 ios9

我收到错误“额外参数”错误“在电话中”我的代码是:

var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)

1 个答案:

答案 0 :(得分:0)

Swift不再使用作为参数传入的错误变量。改为使用do / catch块:

var urlData: NSData?
let request = NSURLRequest() // Presumably declared already
var response: NSURLResponse? // Presumably declared already

do {
    urlData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response)
}
catch let error as NSError {
    print("Error: \(error.localizedDescription)")
}

另请注意,sendSynchronousRequest已弃用,您可能应将其更改为dataTaskWithRequest(request: NSURLRequest) -> NSURLSessionDataTask