Swift 2 to 3, Method Calls, Magic First Parameters & Automatic Conversion Woes

Swift 2 Calling Swift 2

Consider these two methods in Swift 2:

func doThis(thing: AnyObject) {}

func doThisThing(thing thing: AnyObject) {}

are called like this in Swift 2.


doThisThing(thing: "")


  • The first parameter in the doThis method cannot be named by the caller.
  • Whether the method name ends in the first parameter name or not, the behavior doesn't change.

Swift 2 Calling Objective-C

Here are two methods in Objective-C:

- (void) doIt:(NSObject *)thing;

- (void) doThisThing:(NSObject *)thing;

Both of these methods get called without their first parameter name from Swift 2:




  • First parameter name is not used.
  • The method name is the same as the method name in Objective-C.
  • This is true even if the method end in an obvious parameter name, like "doThisWithThing"

Objective-C Calling Swift 2

The method name is the same as that seen in Swift unless you've doubled up on the parameter name as in the above example. So:

func doThis(thing: AnyObject) {}

func doThisThing(thing thing: AnyObject) {}

results in this calling code in Objective-C (note the altered method name in the second example):

[vc doThis:@""];

[vc doThisThingWithThing:@""];

The first method doesn't change even if we use an underscore in the Swift method declaration:

func doThis(_ thing: AnyObject) {}


  • The method names translate normally
  • Doubled-up parameter names get added to the method name in Objective-C with the conjunction "with"

Changes in Swift 3

There is no automatic removal of the first parameter name, so:

func doThisThing(thing: AnyObject) {}

gets called like this:

doThisThing(thing: "" as AnyObject)

If you want to remove the first parameter label for callers, you can use an underscore, of course.

Weird Automatic Conversion to Swift 3

So this converted just fine:

func doThis(thing: AnyObject) {}

func doThisThing(thing thing: AnyObject) {}

// calling code
doThisThing(thing: "what")

and became this in Swift 3

func doThisThing(thing: AnyObject) {}

func doThis(_ thing: AnyObject) {}

// calling code
doThisThing(thing: "what" as AnyObject)
doThis("what" as AnyObject)

So that makes sense.

Converting Again

Now you realize that your massive conversion needs to forward merge more Swift 2 code into the same target. Xcode warns you not to do automatic conversion again, but you do it anyway.

The two methods now look like this, which makes no sense (the second one has now lost its parameter label):

func doThisThing(_ thing: AnyObject) {
    print("doThisThing \(thing.hash)")

func doThis(_ thing: AnyObject) {
    print("doThis \(thing.hash)")

// calling code
doThisThing("what" as AnyObject)
doThis("what" as AnyObject)

In this simple case the callers get updated, but the Objective-C callers do not:

And in many cases, your calling code will be broken in Swift as well.

Calling Objective-C from Swift 2 vs. 3

The automatic madness deepens with Swift 3 interop.

Consider this signature in Objective-C:

+ (NSObject *)deserializeNewOrExistingObjectFromAPIV1JSON:(NSDictionary *)json

In Swift 2 you called it this way:


But this changes in Swift 3 to:

ThatObject.deserializeNewOrExistingObject(fromAPIV1JSON: json.dictionaryObject)

And now the result comes back as a forced optional, whereas in Swift 2 it came back as a non-optional.

Which may or may not get picked up by the automatic converter. The more complex your compilation target is, the less likely the automatic conversion is to succeed.

Some capitalization changes as Objective-C gets magically converted:

- (instancetype)initWithJSON:(NSDictionary *)messageBody
// Swift 2
ThatObject(JSON: dict)!

// Swift 3
ThatObject(json: dict)

And some stuff gets even weirder in Objective-C interop:

+ (id)findForUuid:(NSString *)uuid
// Swift 2

// Swift 3
ThatObject.find(forUuid: tender.uuid)!