由于“内部”保护级别,无法访问“结果”

时间:2019-04-26 04:17:20

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

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

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

}

但是仍然没有解决。

1 个答案:

答案 0 :(得分:0)

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

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

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

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