在swift3中使用objectmapper的alamofire包装器类的问题

时间:2017-06-27 17:55:26

标签: ios swift3 alamofire objectmapper

我正在尝试维护alamofire的包装类以及像这样的对象映射器

import UIKit
import Foundation
import AlamofireObjectMapper
import Alamofire
import ObjectMapper

class APIManager: NSObject {
static let sharedInstance = APIManager()
    public func performRequest<T: Mappable>(_requestURL: URL,method:HTTPMethod, headers: [String: String], parameter: [String: Any],success:@escaping (_ response: DataResponse<[T]>) -> Void, failure:@escaping (Error) -> Void)  {
    Alamofire.request(_requestURL, method: method, parameters: parameter, encoding: JSONEncoding.default, headers:headers)
        .responseObject { (response: DataResponse<T>) in
            print(response.result.value!)
            switch(response.result) {
case .success(_):
                    guard response.result.value != nil

                    else {
                        print("Error")
                        return
                    }
                DispatchQueue.main.async{
                    if response.result.isSuccess {
                        let responseValue = response.result.value
                        success(responseValue as! (DataResponse<[T]>)
)}

                    }
                break
            case .failure(_):
                if let error = response.result.error as? URLError {
                    print("URLError occurred: \(error)")
                      failure(error)
                } else {
                    print("Unknown error: \(String(describing: response.result.error))")
                     failure(response.result.error!)
                }
                break
            }
    }
    }

}

我试图从类中调用这个包装器:

APIManager.sharedInstance.performRequest(_requestURL:loginAccessTokenURL!,方法:.post,headers:Constants.Header,参数:postParameters,success:DataResponse,failure:nil)

这是我从viewdidload调用但是我收到以下错误: Please see the image for error

1 个答案:

答案 0 :(得分:0)

成功是一个回调,所以用以下内容替换它:

APIManager.sharedInstance.performRequest(_requestURL: loginAccessTokenURL!, 
                                         method: .post, 
                                         headers: Constants.Header,
                                         parameters: postParameters,
                                         responseType: AnyObject 
                                         success: { (response) in 
                                         //Call returned back success. Handle Response.                                                                                                                  

                                         }, 
                                         failure: nil)

编辑:

class BaseModel: Mappable {

    required init?(map: Map) {
        mapping(map: map)
    }

    func mapping(map: Map) {

    }
}

class APIManager: NSObject {
    static let sharedInstance = APIManager()
    public func performRequest<T: Mappable>(_requestURL: URL,
                                           method:HTTPMethod, 
                                           headers: [String: String],
                                           parameter: [String: Any],
                                           responseType: AnyObject,
                                           success:@escaping (_ response: AnyObject) -> Void, 
                                           failure:@escaping (Error) -> Void)  {

        Alamofire.request(_requestURL, 
                          method: method, 
                          parameters: parameter, 
                          encoding: JSONEncoding.default,
                          headers:headers).responseJSON { (response: DataResponse<T>) in
                          if response.result == .success {
                              success((responseType as? BaseModel)?.mapping(map: Map.init(mappingType: .fromJSON, JSON: response.result.value)))
                          }

        }
    }
}

我认为这就是你所说的。您的所有JSON模型现在都是subClasses BaseModel。成功返回的对象将是初始化的responseType对象。在您的情况下,responseType将是GetTravelShopsResponse