Alamofire可解码序列化器

时间:2016-09-01 14:34:01

标签: serialization alamofire

我正在尝试更新Alamofire自定义响应序列化程序的一些代码我在cocoa"的位上找到了这段代码。

extension Alamofire.Request {
    public func responseCollection<T: Decodable>(completionHandler: Response<[T], NSError> -> Void) -> Self {
        let responseSerializer = ResponseSerializer<[T], NSError> { request, response, data, error in

            guard error == nil else { return .Failure(error!) }

            let result = Alamofire
                .Request
                .JSONResponseSerializer(options: .AllowFragments)
                .serializeResponse(request, response, data, error)

            switch result {
            case .Success(let value):
                do {
                    return .Success(try [T].decode(value))
                } catch {
                    return .Failure(Error.errorWithCode(.JSONSerializationFailed,
                                                        failureReason: "JSON parsing error, JSON: \(value)"))
                }
            case .Failure(let error): return.Failure(error)
            }
        }

        return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
    }
}

这是pre swift 3,Response<[T], NSError>现在是单值专业化Response<[T]>因此我不确定这个扩展如何转换为对alamofire在swift 3上的更改

我开始更新此代码,这是我得到的

    extension Alamofire.Request {
        public func responseCollection<T: Decodable>(completionHandler: (Response<[T]>) -> Void) -> Self {
            let responseSerializer = ResponseSerializer<[T]> { request, response, data, error in

                guard error == nil else { return .failure(error!) }

                let result = Alamofire
                    .Request
                    .JSONResponseSerializer(options: .allowFragments)
                    .serializeResponse(request, response, data, error)

                switch result {
                case .success(let value):
                    do {
                        return .success(try [T].decode(value))
                    } catch {
                        return .failure(Error(.errorWithCode(.JSONSerializationFailed, failureReason: "JSON parsing error, JSON: \(value)")))
                    }
                case .failure(let error): return.failure(error)
                }
            }

            return response(responseSerializer: responseSerializer, com

pletionHandler: completionHandler)
    }
}

这让我得到2个错误,目前我还没有找到任何解决方法:

1)对于&#34; return .failure(Error(.errorWithCode(.JSONSerializationFailed, failureReason: "JSON parsing error, JSON: \(value)")))&#34;,我收到此错误(&#39;错误&#39;无法构建,因为它没有可访问的初始值设定项)

2)对于&#34; return response(responseSerializer: responseSerializer, completionHandler: completionHandler)&#34;,我收到此错误(无法调用非功能类型的值&#39; HTTPURLResponse&#39;)

希望如果有人能指出我更好的解决方案,那么这段代码或正确的解决方案。谢谢我将继续努力,如果我修复它,我将更新此票。

修改 - 更新

所以这是现在的代码

extension Alamofire.Request {
    public func responseDecodable<T: Decodable>(completionHandler: @escaping (Response<T>) -> Void) -> Self {
        let responseSerializer = ResponseSerializer<T> { request, response, data, error in

            guard error == nil else {
                print("error Network request: \(error)")
                return .failure(error!)
            }

            let result = Alamofire
                .Request
                .JSONResponseSerializer(options: .allowFragments)
                .serializeResponse(request, response, data, error)

            switch result {
            case .success(let value):
                do {
                    let decodableObject = try T.decode(value)
                    return .success(decodableObject)
                } catch let decodeErr {
                    print(decodeErr)
                    let failureReason = "JSON parsing error, JSON: \(value)"
                    let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
                    let error = NSError(domain: "com.prospects.error", code: BackendError.JSONSerializationFailed.rawValue, userInfo: userInfo)
                    return .failure(error)
                }
            case .failure(let error): return.failure(error)
            }
        }

        return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
    }
}

public protocol ResponseObjectSerializable {
    init?(response: HTTPURLResponse, representation: AnyObject)
}

根据上一条评论:Response和ResponseSerializer现在尚未解析,过去常常没有错误。但是今天早上更新了xcode和alamofire,这使得现在出现错误。

0 个答案:

没有答案