使用objectMapper和alamofire在swift 3中进行错误处理

时间:2017-09-09 15:05:26

标签: swift3 alamofire objectmapper

如果从服务器端获取错误,我如何处理完成块中的错误。这是登录的对象映射器类。

  class Login: Mappable {
var result: LoginData?
var success: Bool?
var error: String?

required init?(map: Map){

}

func mapping(map: Map) {

    result <- map["data"]
    success <- map["success"]
    error <- map["error"]

}
}

class LoginData: Mappable {
var name: String?
var title: String?
var token: String?
var imageUrl: String?

required init?(map: Map){

}

func mapping(map: Map) {

    name <- map["name"]
    title <- map["title"]
    token <- map["token"]
    name <- map["name"]
    imageUrl <- map["imageUrl"]
}
}

这是我从视图控制器调用的api。

   func loginMethod(){
    let postData = ["username":loginDict.object(forKey: KUserUserId) as! String,
                    "password": loginDict.object(forKey: KUserPass) as! String]

    userLoginHttp(parameters: postData){ completion in
        self.getUserLoginResponse(result: completion)
    }
}

func getUserLoginResponse(result: LoginData) {
 // Here i do further
}

这是我发布服务的网络类方法

//用户登录网络服务

func userLoginHttp(parameters:Parameters, completion:@escaping (_  
 result:LoginData)->()) {
let defaultObject = UserDefaults.standard
var headerToken = String()
if let x = defaultObject.object(forKey: KDeviceToken)
{
  headerToken = x as! String
}
else{
    headerToken = ""
}

let headers = ["fcmToken": headerToken]

Alamofire.request(KLoginUrl, method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: headers)
    .validate()
    .responseObject{ (response: DataResponse<Login>) in
        switch response.result{
        case .success:
            let value = response.result.value
            completion((value?.result)!)
        case .failure(let error):
            print(error.localizedDescription)
        }
}
}

那么我如何在视图控制器完成块中处理错误?

1 个答案:

答案 0 :(得分:1)

你可以为错误创建另一个处理程序,并在错误情况下调用它

func userLoginHttp(parameters:Parameters,
                       completion:@escaping (_ result:LoginData)->(),
                       errorHandler:@escaping (_ result:Error,_ statusCode:Int?)->()//error handler
        ) {
        let defaultObject = UserDefaults.standard
        var headerToken = String()
        if let x = defaultObject.object(forKey: KDeviceToken)
        {
            headerToken = x as! String
        }
        else{
            headerToken = ""
        }

        let headers = ["fcmToken": headerToken]

        Alamofire.request(KLoginUrl, method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: headers)
            .validate()
            .responseObject{ (response: DataResponse<Login>) in
                switch response.result{
                case .success:
                    let value = response.result.value
                    completion((value?.result)!)
                case .failure(let error):
                    errorHandler(error,response.response?.statusCode)//call error handler
                    print(response.response?.statusCode)
                    print(error.localizedDescription)
                }
        }
    }

使用

func loginMethod(){
    let postData = ["username":loginDict.object(forKey: KUserUserId) as! String,
                    "password": loginDict.object(forKey: KUserPass) as! String]

    userLoginHttp(
        parameters: postData,
        completion:{ completion in
            self.getUserLoginResponse(result: completion)
    },
        errorHandler:{ error,code in
            //do your error stuff
    })
}

func getUserLoginResponse(result: LoginData) {
    // Here i do further
}