我刚开始从Swift2.2->3.0转换我的旧项目。在这个过程中,我不得不将Alamoire更新到4.0版,以获得对新Swift版本的支持。我已经解决了最多的问题,但这是我的不足?
有人能解释为什么上一个返回语句显示以下错误吗?
无法调用非函数类型“httpurlResponse?”的值。
明确地:
返回响应(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)
}

最佳答案

@谢谢你的评论。我写这个答案是因为我还有几个地方要解决我的问题。
下面是我在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>

对于参数来说,@escaping也是必不可少的。

08-19 11:44