我试图用同样的问题查看其他答案,但我仍然无法解决这个问题。请帮我这个天气应用程序的代码。问题出在我的let task = sessionDataTask
行。
import Foundation
protocol WeatherServiceDelegate {
func setWeather(weather: Weather)
}
class WeatherService {
var delegate: WeatherServiceDelegate?
func getWeather(city: String) {
let path = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=79933f5c001b81aecc59976577f8134f"
let url = NSURL(string: path)
let session = URLSession.shared
let task = session.dataTask(with: url) { (data, response, error) in
print(data)
}
task.resume()
}
}
答案 0 :(得分:1)
更改您的
let url = NSURL(string: path)
到
let url = URL(string: path)
这将修复你的代码