swift Alamofire 3的自定义ResponseSerializer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Alamofire 3的自定义ResponseSerializer相关的知识,希望对你有一定的参考价值。

static func CloudFlareResponseSerializer<T: Decodable where T == T.DecodedType>() -> ResponseSerializer<T, CloudFlareError> {
    return ResponseSerializer { (request, response, data, error) in
        guard error == nil else {
            return .Failure(CloudFlareError.NetworkError(error: error!))
        }

        let JSONResult = Request.JSONResponseSerializer().serializeResponse(request, response, data, nil)
        guard case let .Success(responseJSON) = JSONResult else {
            return .Failure(CloudFlareError.SerializationError(error: JSONResult.error!))
        }
        
        let decodedCloudFlareResponse = CloudFlareResponse.decode(JSON(responseJSON))
        guard case let .Success(cloudFlareResponse) = decodedCloudFlareResponse else {
            return .Failure(CloudFlareError.DecodingError(decodedString: decodedCloudFlareResponse.error!.description))
        }
        
        guard cloudFlareResponse.success else {
            return .Failure(CloudFlareError.ResponseError(response: cloudFlareResponse))
        }
        
        let decodedResponseObject = T.decode(cloudFlareResponse.result)
        guard case let .Success(responseObject) = decodedResponseObject else {
            return .Failure(CloudFlareError.DecodingError(decodedString: decodedResponseObject.error!.description))
        }
        
        return .Success(responseObject)
    }
}

以上是关于swift Alamofire 3的自定义ResponseSerializer的主要内容,如果未能解决你的问题,请参考以下文章

Alamofire 的自定义、基于内容的验证器(在 Swift 中)

Alamofire 自定义响应从 Alamofire v1.3 迁移到 3.0(和 Swift 2 语法)

在swift中的自定义标题中获取具有文件大小的下载文件的进度

在 Swift 3.0 中设置 Alamofire 自定义目标文件名而不是使用建议的DownloadDestination

Alamofire.request 不与自定义对象映射

使用逻辑的顺序请求 - Swift 3、Xcode 8、Alamofire 4