Alamofire请求中的响应类类型

时间:2017-05-03 08:04:28

标签: ios swift swift3 alamofire objectmapper

我是一个刚接触swift 3编程的android开发人员,我正在使用Alamofire进行api调用,并避免繁琐的json paring我正在使用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'

有人请帮我解决这个问题,现在已经坚持这个问题了很长一段时间。

2 个答案:

答案 0 :(得分:2)

使用对象映射

继承引用请求
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"]

        }
}

答案 1 :(得分:0)

使用块时无需声明块的参数类型:

Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers)
    .validate()
    .responseData { response in

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

}