user2636197 user2636197 - 2 months ago 36
Swift Question

Alamofire v4 use of undeclared type response

When I updated to Alamofire v4 I got two errors.

The first one is use of undeclared type response which I get from this line:

func responseSavedAdsArray(_ completionHandler: (Response<SavedAdsWrapper, NSError>) ->


The second error is use of unresolved identifier ResponseSerializer which I get from this line:

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


Here is all my code:

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


So what is causing this?

Answer

Change first

func responseSavedAdsArray(_ completionHandler: (Response<SavedAdsWrapper, NSError>) ->

to

func responseSavedAdsArray(_ completionHandler: (DataResponse<SavedAdsWrapper>) ->

EDITED

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

to

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