gpt4 book ai didi

ios - Alamofire 使用未声明的类型 'Serializer'

转载 作者:搜寻专家 更新时间:2023-10-31 23:02:12 25 4
gpt4 key购买 nike

我已经更新到 XCode 7 beta 6 和 Alamofire需要更新到 beta 3。这样做时,我必须更新使用 Alamofire 的代码区域.我特别难以更新的一个领域是用于从指定 URL 检索图像并将其加载到 UIImageView 中的代码。 .

以前,Alamofire 的扩展名处理的是:

extension Alamofire.Request {
class func imageResponseSerializer() -> Serializer {
return { request, response, data in
if data == nil {
return (nil, nil)
}

let image = UIImage(data: data!, scale: UIScreen.mainScreen().scale)

return (image, nil)
}
}

func responseImage(completionHandler: (NSURLRequest, NSHTTPURLResponse?, UIImage?, NSError?) -> Void) -> Self {
return response(serializer: Request.imageResponseSerializer(), completionHandler: { (request, response, image, error) in
completionHandler(request!, response, image as? UIImage, error)
})
}
}

但这不是抛出错误

Use of undeclared type 'Serializer'

我确实意识到 Alamofire不使用 Serializer不再是了,但是有谁知道我在哪里可以找到一些文档或示例在检索图像时现在该做什么?

最佳答案

如您在自述文件中所见,序列化已被重写。

你应该可以使用下面的方法:

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

extension Request {
public func responseObject<T: ResponseObjectSerializable>(completionHandler: Response<T, NSError> -> Void) -> Self {
let responseSerializer = ResponseSerializer<T, NSError> { request, response, data, error in
guard error == nil else { return .Failure(error!) }

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

switch result {
case .Success(let value):
if let
response = response,
responseObject = T(response: response, representation: value)
{
return .Success(responseObject)
} else {
let failureReason = "JSON could not be serialized into response object: \(value)"
let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
return .Failure(error)
}
case .Failure(let error):
return .Failure(error)
}
}

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

关于ios - Alamofire 使用未声明的类型 'Serializer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com