我试图转换对斯威夫特3下面的代码:获取转换NSURLSession错误URLSession斯威夫特卡伦特3
session.dataTaskWithURL(url! as URL, completionHandler: { (data : NSData?, response : URLResponse?, error : NSError?) -> Void in
if error != nil {
callback(items: nil, errorDescription: error!.localizedDescription, placesDetail: [])
}
if let statusCode = response as? NSHTTPURLResponse {
if statusCode.statusCode != 200 {
callback(items: nil, errorDescription: "Could not continue. HTTP Status Code was \(statusCode)", placesDetail: [])
}
}
NSOperationQueue.mainQueue().addOperationWithBlock({() -> Void in
callback(items: GooglePlaces.parseFromData(data!), errorDescription: nil, placesDetail: GooglePlaces.arrayPlaces(data!))
})
}).resume()
所以我这样做:
session.dataTask(with: url!) { (data, response, error) -> Void in
if error != nil {
callback(items: nil, errorDescription: error!.localizedDescription, placesDetail: [])
}
if let statusCode = response as? NSHTTPURLResponse {
if statusCode.statusCode != 200 {
callback(items: nil, errorDescription: "Could not continue. HTTP Status Code was \(statusCode)", placesDetail: [])
}
}
OperationQueue.main.addOperation {
callback(items: GooglePlaces.parseFromData(data!), errorDescription: nil, placesDetail: GooglePlaces.arrayPlaces(data!))
}
}
,但有此错误:
Ambiguous reference to member 'dataTask(with:completionHandler:)'
为什么会出现此错误?
您需要使用URL而不是NSURL –