gpt4 book ai didi

ios - Alamofire 请求中的响应类类型

转载 作者:可可西里 更新时间:2023-11-01 01:21:31 35 4
gpt4 key购买 nike

我是一名 Android 开发人员,刚接触 swift 3 编程,我正在使用 Alamofire为了进行 api 调用并避免繁琐的 json 配对,我正在使用 AlamofireObjectMapper图书馆。我有一个 ApiController,它具有进行 api 调用的功能,下面是相关代码:

public static func makePostRequest<T: Mappable>(url: String, params: Parameters, networkProtocol: NetworkProtocol, responseClass: T){

let headers = getHeaders()

networkProtocol.showProgress()

Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers)
.validate()
.responseObject{
(response: DataResponse<T>) in

switch(response.result){
case .success(_):
networkProtocol.hideProgress()
networkProtocol.onResponse(response: response)
break
case .failure(_):
networkProtocol.hideProgress()
networkProtocol.onErrorResponse(response: response)
break
}

}

下面是我的模型类:

import ObjectMapper

class BaseResponse: Mappable {

required init?(map: Map) {

}

func mapping(map: Map) {

}
}

下面是类调用 api 的函数:

public static func callSomeApi(params: Parameters, networkProtocol: NetworkProtocol){
ApiHelper.makePostRequest(url: AppConstants.URLs.API_NAME, params: params, networkProtocol: networkProtocol, responseClass: BaseResponse)
}

现在错误在下面一行

ApiHelper.makePostRequest(url: AppConstants.URLs.API_NAME, params: params, networkProtocol: networkProtocol, responseClass: BaseResponse)

它说 Argument type 'BaseResponse.Type' does not conform to expected type 'Mappable'

有人请帮我解决这个问题,现在在这个问题上停留了很长一段时间。

最佳答案

这是一个使用对象映射的引用请求

import Alamofire
import ObjectMapper
import AlamofireObjectMapper


func requestUrl<T:Mappable>(url: URLRequestConvertible, withSuccess success:@escaping (_ response: T) -> ()) {
Utils.addHUD()

Alamofire.request(url).validate().responseObject { (response :DataResponse<T>) in
Utils.hideHUD()
guard response.result.isSuccess else{
Utils.showAlert(message: (response.result.error?.localizedDescription)!, action: {

})
return
}
let root:RootClass = response.result.value as! RootClass
if root.status == "Fail"{
Utils.showAlert(message: (root.msg!), action: {

})
}else{

success(response.result.value!)
}
}


}

这是我要映射的类

import ObjectMapper


class RootClass : NSObject, Mappable{

var data : [String : Any]?
var msg : String?
var status : String?


class func newInstance(map: Map) -> Mappable?{
return RootClass()
}

required init?(map: Map){
super.init()
}
private override init(){
super.init()
}

func mapping(map: Map)
{
data <- map["data"]
msg <- map["msg"]
status <- map["status"]

}
}

关于ios - Alamofire 请求中的响应类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43754468/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com