Visit The School

A (Helpful) Alamofire 4 Migration Guide

Alexander Paterson
|
Posted 8 months ago
|
2 minutes
A (Helpful) Alamofire 4 Migration Guide
Struggling to figure out the new Alamofire? Look here

It had been a while since the last time an open-source library had made me want to literally throw my laptop into the ocean; this was until being forced to upgrade to Alamofire 4.0 while moving over to Swift 3. The Swift interpreter is going to trick you into looking for the wrong kind of error.

Extra argument 'method' in call

I'm clearly not the only person who was pulling their hair out with Xcode complaining about an Extra argument 'method' in call, when I creating what looked like an acceptable request object.

Extra argument method in call

@thebluepotato explains it in this issue:

Any time one argument is of the wrong type, the Swift interpreter here believes that you're wrongly using request(urlRequest:URLRequestConvertible) and therefore believes there's an extra method: argument.

So what's the issue? Well, there are actually two.

.JSON is no longer a valid argument to encoding:; the correct replacement is JSONEncoding.default. Also, HTTP method names are now lowercase, so we need to replace .GET with .get. Xcode was never going to tell you this, though.

So here's our happy request initialiser:

let req = Alamofire.request("http://www.google.com", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil)

Upload and progress

The syntax for uploads has changed in Alamofire 4 to read a bit nicer, with a new argument label: to. There's another initially inexplicable error I and others have encountered: Cannot call value of non-function type 'Progress'.

Cannot call value of non-function type Progress

I found it necessary to remove the brackets indicating the call to uploadProgress. Here's a working call:

Alamofire.upload(data, to: "http://www.google.com").uploadProgress { (progress) in
    print(progress)
}



ALEXANDER
PATERSON