Search code examples
swiftcompiler-errorsobjectmapper

xcode8 Alamofire 4 update error "use of undeclared type Response"


after updating to Xcode8 and Alamofire4 I am getting the error 'use of undeclared type Response' on the code "Response<[T]", (which uses ObjectMapper and realm)

class FetchData {
static func get <T: Object> (_ type: T.Type, success:@escaping () -> Void,     fail:@escaping (_ error:NSError)->Void)->Void where T:Mappable, T:Meta {
    Alamofire.request(type.url(), method: .get)
    .responseArray { (response: Response<[T], NSError>) in
    //.responseArray { (response: DataResponse<[T], NSError>) in
            switch response.result {
            case .success(let items):
                autoreleasepool {
                    do {
                        let realm = try Realm()
                        try realm.write {
                            for item in items {
                                realm.add(item, update: true)
                            }
                        }
                    } catch let error as NSError {
                        fail(error: error)
                    }
                }
                success()
            case .failure(let error):
                fail(error: error)
            }
    }
}
}

I had the code working, to map data to realm using Object Mapper, as per this guide; https://blog.hyphe.me/realm-and-alamofire-in-a-effective-harmony/

Changing it to DataResponse did not work. Any ideas?


Solution

  • Due to Alamofire migration guide to version 4.0 Migration Guide, you should use DataResponse, so the commented line of code is actually right but have redundant parameter NSError which is included into DataResponse struct. Fix: .responseArray { (response: DataResponse<[T]>) in