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行)打印出一个错误

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

Swift 3 次更新

URLSession.shared.dataTask()