Xcode Swift 3 NSURLSession.sharedSession

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41268555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:40:09  来源:igfitidea点击:

Xcode Swift 3 NSURLSession.sharedSession

xcodeswift3xcode8nsurlsessionnsurl

提问by Júlíus Gueni

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

我正在尝试更新我用 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)")
    }

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

和 NSURLSession.sharedSession(5th line) 和 NSURLResponse(6th line) 打印出错误

回答by vadian

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

不要在完成处理程序签名中注释类型并使用 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)")
    }

回答by Matthew Becker

Swift 3 update

斯威夫特 3 更新

URLSession.shared.dataTask()

URLSession.shared.dataTask()