当我更新到Alamofire v4时,我遇到了两个错误。
第一个是使用未声明的类型响应,我从这一行得到了:
func responseSavedAdsArray(_ completionHandler: (Response<SavedAdsWrapper, NSError>) ->
第二个错误是使用未解析的标识符ResponseSerializer ,我从这一行得到:
let responseSerializer = ResponseSerializer<SavedAdsWrapper, NSError> { request, response, data, error in
这是我的所有代码:
extension Alamofire.Request {
func responseSavedArray(_ completionHandler: (Response<SavedWrapper, NSError>) -> Void) -> Self {
let responseSerializer = ResponseSerializer<SavedWrapper, NSError> { 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: Dictionary<NSObject, AnyObject> = [NSLocalizedFailureReasonErrorKey: failureReason, Error.UserInfoKeys.StatusCode: response!.statusCode]
let error = NSError(domain: Error.Domain, code: Error.Code.StatusCodeValidationFailed.rawValue, userInfo: userInfo)
return .Failure(error)
}
let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)
switch result {
case .Success(let value):
let json = JSON(value)
let wrapper = SavedWrapper()
wrapper.next = json["saveditems"]["next_page_url"].stringValue
wrapper.previous = json["saveditems"]["prev_page_url"].stringValue
wrapper.count = json["saveditems"]["total"].intValue
var allSaved:Array = Array<Saved>()
print(json)
let results = json["saveditems"]["data"]
print(results)
for jsonAdata in results
{
let adsJData = SavedAds(json: jsonAdata.1, id: Int(jsonAdata.0))
allSaved.append(adsJData)
}
wrapper.saveditems = allSaved
return .Success(wrapper)
case .Failure(let error):
return .Failure(error)
}
}
return response(responseSerializer: responseSerializer,
completionHandler: completionHandler)
}
}
那是什么导致了这个?
答案 0 :(得分:5)
先改变
func responseSavedAdsArray(_ completionHandler: (Response<SavedAdsWrapper, NSError>) ->
到
func responseSavedAdsArray(_ completionHandler: (DataResponse<SavedAdsWrapper>) ->
<强> EDITED 强>
let responseSerializer = ResponseSerializer<SavedAdsWrapper, NSError> { request, response, data, error in
到
let responseSerializer = DataResponseSerializer<SavedAdsWrapper> { request, response, data, error in