2

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

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

}

但它仍然没有修复。

4

1 回答 1

1

这里的根本问题在于您的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
        }
    }
}
于 2019-04-26T06:33:02.170 回答