共享会话的应用程序错误

时间:2016-09-03 15:52:07

标签: ios swift

我已经尝试过尽可能地从教程中转换一些代码,但它似乎没有用,目前,它的给予

  

投掷功能的转换无效

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

1 个答案:

答案 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)
        }
    }
}