我剛剛開始從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)
}
您是否找到任何解決方法?我有完全相同的問題。 – Ryan
是的,雖然編譯器沒有完全告訴我們原因。真正的問題是DataResponse只接受一個模板類而不是NSError/Error。所以它應該是:(DataResponse)而不是(DataResponse ,錯誤?)。 ErrorCode應該改爲使用swift 3 AFError –
Mat0