I have seen this problem several times on this site, but none of the solutions seem to work.
I am expanding the alamofire request to have an array of repo objects as a result; however, I continue to get an error in the title. Here is the code:
extension Alamofire.Request {
class func repoArrayResponseSerializer() -> ResponseSerializer<Array<Repo>, NSError> {
return ResponseSerializer { request, response, data, error in
guard error == nil else { return .Failure(error!) }
guard data != nil else { return .Failure(error!) }
do {
let jsonData: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
let json = JSON(jsonData!)
if json.error != nil || json == nil {
return .Failure(error!)
}
var repos: Array = Array<Repo>()
for (_, jsonRepo) in json {
let repo = Repo(json: jsonRepo)
repos.append(repo)
}
return .Success(repos)
} catch {
return .Failure(error as NSError)
}
}
}
func responseRepoArray(completionHandler: Result<Array<Repo>, NSError> -> Void) -> Self {
return response(responseSerializer: Request.repoArrayResponseSerializer(), completionHandler: completionHandler)
}
}
Any help is appreciated.
source
share