Craig Grummitt – Before I forget

Discoveries in Swift and iOS development.

Func parameters in Swift 3.0

·

Well, Apple’s done it again, and made func parameters even simpler in Swift 3.0!

It’s all very straight forward now, class methods, static methods, global functions, init methods – they all work the same now, hooray!

1. Explicit parameter names

ALL parameter names by default are explicit:

[sourcecode language=”objc”]
class Person {
init(firstName:String,surname:String) {
(firstName,surname)
}
}
var person = Person(firstName: “Tim”, surname: “Cook”)
[/sourcecode]

2. External parameter name

The same parameter can 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.

[sourcecode language=”objc”]
class Person {
init(firstName 名字:String,surname 姓:String) {
(名字,姓)
}
}
var person = Person(firstName: “Tim”, surname: “Cook”)
[/sourcecode]

3. 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:

[sourcecode language=”objc”]
class Person {
init(_ firstName:String,_ surname:String) {
(firstName,surname)
}
}
var person = Person(“Tim”, “Cook”)
[/sourcecode]

For more on functions in Swift, check out my video:
[youtube https://www.youtube.com/watch?v=LDWYTsJhVfE&w=560&h=315]


Comments

3 responses to “Func parameters in Swift 3.0”

  1. […] This post has been updated for Swift 3.0 here. […]

  2. […] ActionSwift3 uses a combination of the Swift and ActionScript approaches. You can read more about function parameters in Swift here. […]

Leave a Reply

Your email address will not be published. Required fields are marked *