0
A
回答
2
在SWIFT 3 NS前缀去掉,现在下面是urlsession
URLSession.shared.dataTask(with: url!) { (data, response, error) in
//do the work
}
2
语法在迅速3 - 最新的方法调用的API。
let url = URL(string: urlString)! //Pass the URL String format
var request = URLRequest(url: url)
_ = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
self.ProgressViewHide()
return
}
DispatchQueue.main.async {
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
// Get Response on responseJSON
}
}.resume()
1
因此,对于Swift 3.x中的新方法,您必须像这样放置。另外,请记住,你的闭包是异步工作的,所以我用GCD封装了要在主线程上执行的调用。
let request = URLRequest(url: URL(string: "Put your url")!)
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
//print(error?.localizedDescription)
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as AnyObject
//on main thread
DispatchQueue.main.async {
self.currentCode = json["code"]!! as! String
}
}catch{
print("error\(error)")
}
}
task.resume()
不过,我建议您使用更简便的方法来处理使用Alamofire网络, 的API调用https://github.com/Alamofire/Alamofire
相关问题
- 1. didSelectRowAt方法未被调用(swift 3)
- 2. 将json参数发送到api使用post方法Swift 3
- 3. iOS 10,Swift 3 API调用SSL错误
- 4. 使用Swift 3调试Firebase
- 5. 更新Alamofire使用Swift 3
- 6. 从.NET调用Java API - 最佳方法
- 7. 使用Swift 3和alamofire的Firebase API 4
- 8. 使用POST方法调用REST API调用GET方法
- 9. 如何使用Swift 3处理很多API调用GCD
- 10. 未调用Swift audioPlayerDidFinishPlaying方法
- 11. Swift 3 - 有什么办法强制方法调用对象?
- 12. Swift 3 getter方法
- 13. ViewForAnnotation无法在Swift 3中调用
- 14. 使用WebAssembly调用Web API方法
- 15. api调用的URL方法
- 16. 新的FUITableViewDataSource - 如何使用? Swift 3
- 17. Swift 3从类不使用#selector语法的方法
- 18. 使用swift进行API调用
- 19. uploadTask方法不起作用URLSession Swift 3
- 20. 调用方法的最快方法
- 21. 从AppDelegate调用GameScene方法(Swift 3,SpriteKit,Xcode 8)
- 22. 在swift中没有调用UITableView委托方法3
- 23. 类别调用swift 3
- 24. Rails 3 api - 最常用方法的位置
- 25. 在swift中调用childcontroller的parentController方法
- 26. Swift:调用可选变量的方法
- 27. Rails 3弃用的方法和API
- 28. 如何使web API Post方法调用
- 29. 如何在swift 3中使用平移手势的方法?
- 30. 在swift 3中调用mutlile API的问题?
使用Alamofire框架 –