Xcode Swift 3 NSURLSession.sharedSession

时间:2016-12-21 17:27:57

标签: xcode swift3 xcode8 nsurlsession nsurl

我试图用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行)打印出错误

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

Swift 3更新

URLSession.shared.dataTask()