【问题标题】:'result' is inaccessible due to 'internal' protection level由于“内部”保护级别,“结果”无法访问
【发布时间】:2019-09-15 13:30:48
【问题描述】:

我想使用 URLSession 为通用 API 请求创建自定义框架。所以我使用了this 链接。我将使用这个项目作为自定义框架。因此,为了使用它,我已经通过 open 更改了它的访问说明符。并且使用 thislink 我已经将它导入到我的项目中。我已经完成了以下代码来调用发布请求

import iOSCoreFramework

func callBySpeedyNetworking2() {
    let trylogin = login(username: "****", password: "***")
    SpeedyNetworking.removeToken()
    SpeedyNetworking.postUrl(url: URL(string: GlobalConstants.loginFullURL), model: trylogin) { (response) in
        if !response.success {
            // show a network error
            print("network error ",response.error)
            return
        }
        // successful
        print("RESPONSE 1 ------------> ")
        dump(response.result(model: ModelResponse.self))
        dump(response.jsonResults(model: ModelResponse.self))
    }
}

但它给了我一个关于“成功”、“错误”和以下几行的错误:

dump(response.result(model: ModelResponse.self))
dump(response.jsonResults(model: ModelResponse.self))

从各种链接中,我通过以下方式对SpeedyResponse 类进行了更改

public class SpeedyResponse {

public var success : Bool!
public var statusCode = 0
public var error: Error?
public var data: Data?
public init (success : Bool, statusCode : Int,error :  Error, data : Data){
    self.success = success
    self.statusCode = statusCode
    self.error = error
    self.data = data
}

public init(data: Data?, response: URLResponse?, error: Error?) {
    self.error = error
    self.data = data

    if let httpResponse = response as? HTTPURLResponse {
        statusCode = httpResponse.statusCode
    }

    success = statusCode == 200 && error == nil && data != nil ? true : false
}

public func jsonResults<T>(model: T.Type) -> T? {
    if !success { return nil }
    guard let responseData = data else { return nil }

    do {
        return try JSONSerialization.jsonObject(with: responseData) as? T
    } catch {
        return nil
    }
}

public func result<T: Decodable>(model: T.Type) -> T? {
    if !success { return nil }
    guard let responseData = data else { return nil }

    do {
        return try JSONDecoder().decode(model, from: responseData)
    } catch {
        return nil
    }
}

}

但它仍然没有修复。

【问题讨论】:

  • 我面临同样的问题。任何人都可以知道实际问题吗?

标签: swift access-control swift-custom-framework


【解决方案1】:

这里的根本问题在于您的struct 内部,所有变量和方法都自动声明,其范围为internal。因此,当您创建这样的类型时:

public class Human {
    let foo: String
    let bar: String
}

您将无法同时访问foo 和bar,因为它们实际上被声明为:

public class Human {
    internal let foo: String
    internal let bar: String
}

要解决此问题,只需将访问修饰符添加到 public。


从这个意义上说,您的新模型应该如下所示

public class SpeedyResponse {

    public var success: Bool!
    public var statusCode = 0
    public var error: Error?
    public var data: Data?

    public init(data: Data?, response: URLResponse?, error: Error?) {
        self.error = error
        self.data = data

        if let httpResponse = response as? HTTPURLResponse {
            statusCode = httpResponse.statusCode
        }

        success = statusCode == 200 && error == nil && data != nil ? true : false
    }

    public func jsonResults<T>(model: T.Type) -> T? {
        if !success { return nil }
        guard let responseData = data else { return nil }

        do {
            return try JSONSerialization.jsonObject(with: responseData) as? T
        } catch {
            return nil
        }
    }

    public func result<T: Decodable>(model: T.Type) -> T? {
        if !success { return nil }
        guard let responseData = data else { return nil }

        do {
            return try JSONDecoder().decode(model, from: responseData)
        } catch {
            return nil
        }
    }
}

【讨论】:

  • 感谢您的回复。我也这样做了,但没有成功
  • 我已做出建议的更改。请检查,但仍然出现相同的错误
  • 您确定使用的是刚刚创建的框架的更新版本吗?
  • 如果没有更多关于您如何实际设置项目的上下文,我将无法在这里提供太多帮助。上面的代码应该可以工作。
猜你喜欢
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多