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:

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

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.

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

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:

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

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

Tagged with:
Posted in Swift
3 comments on “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

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: