为openweathermap设置API。但是,在设置时:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
lat = location.coordinate.latitude
lon = location.coordinate.longitude
AF.request("http://api.openweathermaps.org/data/2.5/weather?lat=\(lat)&lon=\(lon)&appid=\(apiKey)&units=metric").responseJSON {
response in
self.activityIndicator.stopAnimating()
if let responseStr = response.result.value {
let jsonResponse = JSON(responseStr)
let jsonWeather = jsonResponse["weather"].array![0]
let jsonTemp = jsonResponse["main"]
let iconName = jsonWeather["icon"].stringValue
}
}
}
我收到错误:
由于“内部”保护水平,“价值”无法进入
感谢您尝试Alamofire 5!这个错误有点误导,因为Swift编译器试图提供帮助,让你知道internal
上有一个你无法访问的value
属性response.result
。然而,这是一个内部Alamofire扩展,因为我们移动到Alamofire 5 beta 4的Result
type provided by the Swift standard library。系统Result
不提供Alamofire之前提供的value
类型的error
和Result
属性。因此,虽然我们在内部具有扩展功能以向我们提供功能,但它们并非公开存在以供您的应用使用。
这里的终极解决方案取决于您。您可以自己扩展Result
以提供属性(随意使用Alamofire实现),或者您可以不使用switch
值的属性和response.result
来提取响应值。我建议现在使用switch
,因为它迫使你考虑.failure
案件。
在最新的beta 4
版本中,Alamofire转而使用新的标准Result类型,因此我们过去使用的便利属性已经内部化。现在你可以像这样切换结果:
switch response.result {
case let .success(value): ...
case let .failure(error): ...
}
或者您可以在自己的项目中进行类似的扩展。他们将不再公开提供扩展。