swift – session.dataTaskWithURL completionHandler从未调用过

前端之家收集整理的这篇文章主要介绍了swift – session.dataTaskWithURL completionHandler从未调用过前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码
  1. let urlPath:String = apiURL + apiVersion + url + "?api_key=" + apiKey
  2. let url = NSURL(string: urlPath)
  3. let session = NSURLSession.sharedSession()
  4. println(url!)
  5. let task = session.dataTaskWithURL(url!,completionHandler: {(data,reponse,error) in
  6. println("Task completed")
  7. // rest of the function...
  8. })

永远不会调用completionHandler函数.我尝试在浏览器中调用URL,它运行正常.我试过另一个URL,它仍然无法正常工作.我检查了我的ios模拟器可以连接到互联网,它确实.

我不知道为什么函数没有被调用,因为我没有任何错误,所以很难调试.

任务永远不会完成,因为它永远不会开始.您必须使用resume()方法手动启动数据任务.
  1. let urlPath = apiURL + apiVersion + url + "?api_key=" + apiKey
  2. let url = NSURL(string: urlPath)!
  3. let session = NSURLSession.sharedSession()
  4.  
  5. let task = session.dataTaskWithURL(url) { data,response,error in
  6. print("Task completed")
  7. // rest of the function...
  8. }
  9.  
  10. task.resume()

猜你在找的Swift相关文章