我对于macOS应用程序的这段代码有问题。:
URLSession.shared.dataTask(with: URL(string: "https://api.openweathermap.org/data/2.5/weather?q=\(city)&appid=xxxxxxxxxxxxxxxxxxxxxxxxxxxx")!) { data, response, error in
if let error = error {
print("Error:\n\(error)")
} else {
do {
let weather = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String: AnyObject]
self.weatherLabel.stringValue = "Temperature \(weather["main"]!["temp"]!!)°C Humidity \(weather["main"]!["humidity"]!!)% Pressure \(weather["main"]!["pressure"]!!)hPa."
} catch let jsonError as NSError {
print("JSON error:\n\(jsonError.description)")
}
}
}.resume()
当尝试获取天气数据时,会出现错误消息:
Error Domain = NSURLErrorDomain代码= -1003“具有指定地址的服务器 找不到主机名。” UserInfo = {NSUnderlyingError = 0x600000d076c0 {Error Domain = kCFErrorDomainCFNetwork代码= -1003“(空)” UserInfo = {_ kCFStreamErrorCodeKey = -72000, _kCFStreamErrorDomainKey = 10}},NSErrorFailingURLStringKey = https://api.openweathermap.org/data/2.5/weather .........
我尝试了许多不同的方法,但是都没有成功。。有人知道如何更正此功能吗?