iOS Swift 2 - 失败时 Alamofire 打印响应值

标签 ios swift alamofire

我正在为我的 http 请求使用 Alamofire,但我无法从未通过验证的请求中获取错误消息

Alamofire.request(method, url, headers: headers, parameters: parameters, encoding: encoding)
            .validate(statusCode: 200..<300)
            .responseJSON { response in

                switch response.result {
                case .Success:
                    // response.result.value returns JSON

                case .Failure(let error):
                    // response.result.value returns nil
                }
        }

如果出现错误代码 400 和其他错误代码,我如何获取数据 (JSON)。即使请求不成功,API 也会在正文中发送数据。

最佳答案

按照 SaiCYLi,获取响应数据的唯一方法是避免使用验证。
请参阅 Alamofire 中的 Result.swift。
有拦截器。

public var value: Value? {
    switch self {
    case .Success(let value):
        return value
    case .Failure:
        return nil
    }
}

我想给你评论而不是回答。但是我的声望不到 50。抱歉。

关于iOS Swift 2 - 失败时 Alamofire 打印响应值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34862773/

相关文章:

ios - 自定义 UIView 类 - Swift

ios - UIAccessibilityContainer 动态更新协议(protocol)方法

ios - 将图像从 UITabBarController 传递到 subview (Swift)

ios - 将 NSPrivateQueueConcurrencyType 子上下文与 NSPrivateQueueConcurrencyType 父上下文一起使用

ios - 构建成功后将文本文件安装/导入到物理设备时 Xcode 崩溃 (RangeException)

ios - 如何将字符串解析为 NSTimeInterval

ios - 如何在 2 个堆栈 View 之间设置标签的自动布局?

ios - AlamoFire 发送 Nil

swift - 如何使用 alamofire 将服务的响应存储在模型中

ios - Swift - JSON 写入中的顶级类型无效