我想使用 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
}
}
}
但它仍然没有修复。