由于“内部”保护级别,无法访问“值” [英] 'value' is inaccessible due to 'internal' protection level
问题描述
设置openweathermap的API。但是,要进行以下设置:
Setting up API for openweathermap. However, when it comes to setting up this:
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
}
}
}
我收到错误:
'值'由于内部保护级别而无法访问
'value' is inaccessible due to 'internal' protection level
推荐答案
感谢尝试Alamofire 5!这个错误有点让人误解,因为Swift编译器试图提供帮助,并让您知道存在内部
属性 value
在您无法访问的 response.result
上。但是,这是内部的Alamofire扩展,因为我们移至 Alamofire 5 beta 4中Swift标准库提供的 Result
类型。系统 Result
不提供Alamofire先前提供的结果$的
值
和错误
属性c $ c>类型做了。因此,尽管我们内部有一些扩展可以为我们提供功能,但它们并不公开存在供您的应用使用。
Thanks for trying Alamofire 5! This error is a bit misleading, as the Swift compiler is trying to be helpful and letting you know there's an internal
property value
on response.result
that you can't access. However, this is an internal Alamofire extension, as we moved to the Result
type provided by the Swift standard library in Alamofire 5 beta 4. The system Result
does not offer the the value
and error
properties that Alamofire's previously provided Result
type did. So while we have extensions internally to provide functionality to us, they do not exist publicly for use by your app.
最终的解决方案取决于您。您可以自己扩展 Result
以提供属性(随意使用Alamofire实现),也可以不使用属性和 switch $ c在
response.result
值上加上$ c>以提取响应值。我建议暂时使用 switch
,因为它会迫使您考虑 .failure
的情况。
The ultimate solution here is up to you. You can extend Result
yourself to offer the properties (feel free to use the Alamofire implementation) or you can do without the properties and switch
over the response.result
value to extract the response value. I would suggest using switch
for now, as it forces you to consider the .failure
case.
这篇关于由于“内部”保护级别,无法访问“值”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!