8

Updating to Alamofire 4.5 broke the syntax. How should I reformat my code in order for it to work?

What I have:

func getAllBeacons(completionHandler: @escaping ([BeaconModel]) -> ()) {
        let URL = "https://testwebapi.knowe.net/Knowe.Beacon.WebApi/beacon/GetAllByLanguage"
        let preferredLanguage = NSLocale.preferredLanguages[0]
        print(UIDevice.current.modelName)
        AF.request(URL, method: .post, parameters: ["SearchValue": preferredLanguage, "IosModelName": UIDevice.current.modelName]).responseArray { (response: DataResponse<[BeaconModel]>) in
            let beaconArray = response.result.value
            completionHandler(beaconArray!)
        }
    }

What I had:

func getAllBeacons(completionHandler: @escaping ([BeaconModel]) -> ()) {
        let URL = "https://testwebapi.knowe.net/Knowe.Beacon.WebApi/beacon/GetAllByLanguage"
        let preferredLanguage = NSLocale.preferredLanguages[0]
        print(UIDevice.current.modelName)
        Alamofire.request(URL, method: .post, parameters: ["SearchValue": preferredLanguage, "IosModelName": UIDevice.current.modelName]).responseArray { (response: DataResponse<[BeaconModel]>) in
            let beaconArray = response.result.value
            completionHandler(beaconArray!)
        }
    }

Strangely this code works when I run it on the emulator, but not on my physical iPhones. The latter gives me an error: Module 'Alamofire' has no member named 'request'

This project was asigned to me, and I don't know what versions of Alamofire and Alamofireobjectmapper were used. The best case scenario would be to downgrade to the former versions but I don't know what versions will be compatible with the former syntax.

I'm using Xcode 11.3.1 and Swift

  pod 'Alamofire', '~> 4.5'
  pod 'AlamofireObjectMapper', '~> 5.0'
  pod 'NVActivityIndicatorView'
  pod 'SQLite.swift', '~> 0.11.4'

1 Answers1

5

Alamofire 5 changed the various *Response types to be doubly generic. That is, generic to both the Success and Failure types. In your case, your DataResponse needs to provide an Error type which is produced in failure cases. Alamofire 5 returns the AFError type by default, but since responseArray is custom, there may be different error types in use.

As an aside, the pod definitions you've provided should not have been able to upgrade to Alamofire 5, so I'm not sure how you've encountered this issue.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37
  • 2
    Any idea how to handle this doubly generic> – Anuraj Apr 01 '20 at 12:29
  • 1
    @Anuraj You can use the `AFDataResponse` or `AFDownloadResponse` type to make it singly generic with an `AFError` error type. But if you want a different error type you'll need to deal with both types. – Jon Shier Apr 01 '20 at 15:35
  • In lower version of Alamofire i used this code request.response { (response:DataResponse) in }. Where responseType was a generic. When using the latest one the following error is getting Generic type 'DataResponse' specialized with too few type parameters (got 1, but expected 2). And when I use this ----> request.response { (response:AFDataResponse) in } getting error cannot convert value of type '(DataResponse) -> ()' to expected argument type '(AFDataResponse) -> Void' (aka '(DataResponse, AFError>) – Anuraj Apr 01 '20 at 16:51