我已经尝试过尽可能地从教程中转换一些代码,但它似乎没有用,目前,它的给予
投掷功能的转换无效
在let task
行。
func request(url:String, callback:(NSDictionary) -> ()) {
do{
var nsURL = NSURL(string: url)
let task = try NSURLSession.sharedSession().dataTaskWithURL(nsURL!){
(data, response, error) in
var error:NSError?
var response = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as NSDictionary
callback(response)
}
task.resume()
}
catch{
print(error)
}
}
答案 0 :(得分:0)
从try
删除NSURLSession.sharedSession
。
func request(url:String, callback:(NSDictionary) -> ()) {
let url = NSURL(string: url)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url) { data, response, error in
var error:NSError?
do {
var response = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
callback(response)
}catch {
print(error)
}
}
}