2016-09-20 54 views
3

我剛剛開始從swift 2.2-> 3.0轉換我的舊項目。在這個過程中,我不得不將Alamofire更新到4.0版以獲得對新版Swift版本的支持。我已經修好了最多,但是我現在不了了之。無法調用非函數類型'HTTPURLResponse?'的值 - Alamofire 4.0

任何人都可以解釋爲什麼在過去return語句顯示以下錯誤:

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) 
} 
+0

您是否找到任何解決方法?我有完全相同的問題。 – Ryan

+0

是的,雖然編譯器沒有完全告訴我們原因。真正的問題是DataResponse只接受一個模板類而不是NSError/Error。所以它應該是:(DataResponse )而不是(DataResponse ,錯誤?)。 ErrorCode應該改爲使用swift 3 AFError – Mat0

回答

3

@ 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> 

而且@escaping是至關重要的completionHandler參數。

相關問題