Xcode Swift 3 NSURLSession.sharedSession(Xcode Swift 3 NSURLSession.sharedSession)

编程入门 行业动态 更新时间:2024-10-24 16:23:10
Xcode Swift 3 NSURLSession.sharedSession(Xcode Swift 3 NSURLSession.sharedSession)

我试图用swift 2更新我的基本天气应用程序到swift 3这是我的代码:

func getWeather(city: String) { let path = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=095711373620570aa92ee530246dc8af" let url = NSURL(string: path) let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(url!) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in print(">>>> \(data)") }

和NSURLSession.sharedSession(第5行)和NSURLResponse(第6行)打印出错误

Im trying to update my basic weather app i made with swift 2 to swift 3 this is my code:

func getWeather(city: String) { let path = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=095711373620570aa92ee530246dc8af" let url = NSURL(string: path) let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(url!) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in print(">>>> \(data)") }

and NSURLSession.sharedSession(5th line) and NSURLResponse(6th line) print out an error

最满意答案

不要在完成处理程序签名中注释类型并使用Swift 3本机结构:

func getWeather(city: String) { let path = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=095711373620570aa92ee530246dc8af" let url = URL(string: path) let session = URLSession.shared let task = session.dataTask(with:url!) { (data, response, error) -> Void in print(">>>> \(data)") }

Don't annotate types in the completion handler signature and use the Swift 3 native structures:

func getWeather(city: String) { let path = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=095711373620570aa92ee530246dc8af" let url = URL(string: path) let session = URLSession.shared let task = session.dataTask(with:url!) { (data, response, error) -> Void in print(">>>> \(data)") }

更多推荐

本文发布于:2023-07-19 01:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swift   Xcode   sharedSession   NSURLSession

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!