Func parameters in Swift 2.0

This post has been updated for Swift 3.0 here.

This week func parameters were simplified in Swift 2.0. The hash symbol was deprecated for specifying a parameter should be explicitly named, and the rules were simplified concerning global functions vs class methods. Here’s the new state of play:

1. init methods

Init methods require ALL parameter names to be explicit when instantiating an object:

class Person {
    init(firstName:String,surname:String) {
        (firstName,surname)
    }
}
var person = Person(firstName: "Tim", surname: "Cook")

external parameter name

The same parameter can of course have a different name externally and internally. This could be useful if different names make more sense depending on the perspective, or even for different human languages – perhaps the developer speaks a different language to the user of the class. The external parameter name comes first.

class Person {
    init(firstName 名字:String,surname 姓:String) {
        (名字,姓)
    }
}
var person = Person(firstName: "Tim", surname: "Cook")

implicit external parameter name

If for some reason you want your external parameter names to be implicit, you can use this external parameter name construct, and represent them with underscores:

class Person {
    init(_ firstName:String,_ surname:String) {
        (firstName,surname)
    }
}
var person = Person("Tim", "Cook")

2. Global functions / class methods

Global functions and class methods now work the same, and this should look familiar to Objective C developers. The first parameter has NO external parameter name by default, but following parameters HAVE external names by default. For this reason the first parameter name is often recommended to be incorporated into the method/func name following the word ‘With’.

func getRectAreaWithWidth(width:Double,height:Double)->Double {
    return width * height
}
getRectAreaWithWidth(10, height: 10)

For the second,third etc parameters, you can of course implement alternative external parameter names, or implicit external parameter names as described above.

force external parameter name

If you want to force your first parameter to have an external name, you need to use the same external parameter construct, and just repeat it:

func getRectArea(width width:Double,height:Double)->Double {
    return width * height
}
getRectArea(width: 10, height: 10)

Interactive developer, Author - iOS development with Swift - book coming 2017 https://manning.com/books/ios-development-with-swift

Tagged with:
Posted in Swift
One comment on “Func parameters in Swift 2.0
  1. […] STOP PRESS: Updated for Swift 2.0 here! […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: