gpt4 book ai didi

swift - 无法调用非函数类型的值 'HTTPURLResponse?' - Alamofire 4.0

转载 作者:搜寻专家 更新时间:2023-10-31 22:30:21 26 4
gpt4 key购买 nike

我刚开始从 swift 2.2->3.0 转换我的旧项目。在这个过程中,我不得不将 Alamofire 更新到 4.0 版以获得对新 swift 版本的支持。我已经修复了大部分问题,但我还没有解决这个问题?

谁能解释一下为什么最后的返回语句显示以下错误:

Cannot call value of non-function type 'HTTPURLResponse?'

具体来说:

return response(responseSerializer: responseSerializer, completionHandler: completionHandler)

extension Alamofire.DataRequest {
func responseTVArray(_ completionHandler: @escaping (DataResponse<TVWrapper>, Error?) -> Void) -> Self {
let responseSerializer = DataResponseSerializer<TVWrapper> { request, response, data, error in
guard error == nil else { return .failure(error!) }

guard let responseData = data else
{
let failureReason = "Array could not be serialized because input data was nil."

let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
let error = NSError(domain: "UMAT", code: ErrorCode.DataSerializationFailed.rawValue, userInfo: userInfo)

return .failure(error)
}

let JSONResponseSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)

switch result {
case .success(let value):
let json = SwiftyJSON3.JSON(value)
let wrapper = TVWrapper()
wrapper.page = json["page"].intValue
wrapper.totalPages = json["total_pages"].intValue
wrapper.totalResults = json["total_results"].intValue

var allTV:Array = Array<TV>()

let results = json["results"]

for jsonTV in results
{
let tv = TV(json: jsonTV.1, id: Int(jsonTV.0) )
if (tv.posterPath == "")
{
continue
}
else
{
allTV.append(tv)
}

}
wrapper.results = allTV
return .success(wrapper)
case .failure(let error):
return .failure(error)
}
}

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

最佳答案

@Mat0 感谢您的评论。我写这个答案是因为在我的案例中我还有几个地方需要修复。

下面是我在Swift 2.2中的方法.

func responseSLJSON(completionHandler: Response<AnyObject, NSError> -> Void,
errorHandler: (String, Result<AnyObject, NSError>) -> NSError = Request.slError()) -> Self {

let responseSerializer = ResponseSerializer<AnyObject, NSError> {
request, response, data, error in

let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)

guard error == nil else {
let errorResult = JSONSerializer.serializeResponse(request, response, data, nil)
return .Failure(errorHandler(#function, errorResult))
}

return JSONSerializer.serializeResponse(request, response, data, error)
}

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

然后我将其转换为...

func responseSLJSON(completionHandler: @escaping (DataResponse<Any>) -> Void,
errorHandler: @escaping (String, Result<Any>) -> NSError = DataRequest.slError()) -> Self {

let responseSerializer = DataResponseSerializer<Any> {
request, response, data, error in

let jsonSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)

guard error == nil else {
let errorResult = jsonSerializer.serializeResponse(request, response, data, nil)
return .failure(errorHandler(#function, errorResult))
}

return jsonSerializer.serializeResponse(request, response, data, error)
}

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

return response(responseSerializer: responseSerializer, completionHandler: completionHandler)在这种情况下是关于参数类型的。我不得不使用 Any而不是 AnyObject因为jsonResponseSerializer返回 DataResponseSerializer<Any> .

public static func jsonResponseSerializer(
options: JSONSerialization.ReadingOptions = .allowFragments)
-> DataResponseSerializer<Any>

还有 @escapingcompletionHandler 至关重要参数。

关于swift - 无法调用非函数类型的值 'HTTPURLResponse?' - Alamofire 4.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39602347/

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