'value' is inaccessible due to 'internal' protection level

瘦欲@ 提交于 2019-12-13 03:38:38

问题


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
            }
        }

    }

I get the error:

'value' is inaccessible due to 'internal' protection level


回答1:


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.

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.




回答2:


In the latest beta 4 version Alamofire switched to using the new standard Result type, so the convenience properties we used to have have been made internal. Now you can switch over the result like this:

switch response.result {
    case let .success(value): ...
    case let .failure(error): ...
}

Or you can make similar extensions in your own project. They won't be offering the extensions publicly anymore.



来源:https://stackoverflow.com/questions/55484714/value-is-inaccessible-due-to-internal-protection-level

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!