Alamofire:如何处理和分类错误?

时间:2016-01-15 16:35:17

标签: swift alamofire

Alamofire的一个例子是:

val paramGrid = new ParamGridBuilder()
 .addGrid(hashingTF.numFeatures, Array(10, 100, 1000))
 .addGrid(naiveBayes.lambda, Array(0.1, 0.01))
 .build()

如何处理“网络中断”,“404”,“未找到服务器”等错误,为用户提供无法解决其原因的信息?

1 个答案:

答案 0 :(得分:2)

您可以获取http状态代码并将其处理为:

Alamofire.request(.GET, USERS_URL, headers: headers, encoding: .JSON)
        .responseJSON { response in
            debugPrint(response)
            switch response.result {
            case .Success(let data):
                if response.response!.statusCode == 200 {
                  //do things
                }else if response.response!.statusCode == 401 {
                    //do things
                }
            case .Failure(let error):
                print("Request failed with error: \(error)")
            }
    }