【问题标题】:'value' is inaccessible due to 'internal' protection level由于“内部”保护级别,“值”不可访问
【发布时间】:2019-04-02 23:05:30
【问题描述】:

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

    }

我得到错误:

“值”由于“内部”保护级别而无法访问

【问题讨论】:

  • 我之前也遇到过这个问题,你试过把这个功能公开吗?
  • 你是怎么做到的?

标签: swift alamofire


【解决方案1】:

感谢您试用 Alamofire 5!这个错误有点误导,因为 Swift 编译器试图提供帮助并让您知道 response.result 上有一个您无法访问的 internal 属性 value。然而,这是一个内部 Alamofire 扩展,因为我们在 Alamofire 5 beta 4 中移至 Result type provided by the Swift standard library。系统 Result 不提供 Alamofire 之前提供的 Result 类型提供的 valueerror 属性.因此,虽然我们在内部有扩展来为我们提供功能,但它们并不公开存在以供您的应用使用。

这里的最终解决方案取决于您。您可以自己扩展Result 以提供属性(随意使用Alamofire 实现),或者您可以在response.result 值上不使用属性和switch 以提取响应值。我建议现在使用switch,因为它迫使您考虑.failure 的情况。

【讨论】:

  • 这在 Alamofire 5 beta 7 中已经改变:我们的内部扩展不再使用 value / error 属性,所以这个错误应该不再发生。相反,您应该看到一个正常的“没有这样的属性”错误。
【解决方案2】:
switch response.result {
        
        case .success(let value):
            print("Alamo value: \(value)")
            break
        case .failure(let error):
            print("Alamo error: \(error)")
            break
        }

【讨论】:

    【解决方案3】:

    在最新的beta 4 版本中,Alamofire 切换到使用新的标准 Result 类型,因此我们过去的便利属性已被内部化。现在您可以像这样切换结果:

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

    或者您可以在自己的项目中进行类似的扩展。他们将不再公开提供扩展。

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 2017-12-23
      • 2021-08-24
      • 1970-01-01
      相关资源
      最近更新 更多