Reputation:
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'
Upvotes: 9
Views: 6855
Reputation: 12770
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.
Upvotes: 5