Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Functions and Optionals Parameters and Tuples Named Parameters

Tommy Bergeron
Tommy Bergeron
1,842 Points

In El Capitan / Xcode 7 the shortcut syntax #person: doesn't work anymore. "person person:" has to be typed

I've looked all over the Swift 2 notebook on iBooks and didn't find anything yet.

Here's what happens when you try to use it: http://d.pr/i/14o4I

Anybody has an idea what is the new updated syntax?


func greeting(person: String) {
    println("Hello \(person)")

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Tommy,

As explained in the error, named parameters are no longer a part of the Swift programming language (as of Swift 2.0) which means we now only need to rely on parameter labels which go before the actual parameter variable. The error message explains the simple way of fixing this, that is as shown in your searchNames function, which is you have name name: String.

In that function the first name is the parameter label, the second name is the parameter variable; and then we have the typical String type, however, like in all previous versions of Swift we don't need to use the same name for both, instead we can call the label anything and simply use the variable within the function which can make things simpler in the long run.

With the above code, we can make it behave the same way as a named parameter by doing the below, which as I explained is the quick and simple solution.

func greeting(person person: String) {
    println("Hello \(person)")

Now we can simply call our greeting function like so.

greeting(person: "Chris")

However, this is not the most pleasant way of writing labels since we want to try and be as explicit as possible so we and others know exactly what the code wants and does. Best practises advise the below is correct.

func greeting(nameOfPerson person: String) {
    println("Hello \(person)")

greeting(nameOfPerson: "Chris")

It pretty much comes down to what you feel comfortable with, but hopefully that answers your question.

Happy coding!

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Tommy: Besides what Chris just explained, I would like to point out that the Swift book in iTunes explains this very very well. Here are some of their explanations.

Apple Inc. “The Swift Programming Language (Swift 2 Prerelease).” iBooks. https://itun.es/us/k5SW7.l

Function Parameter Names

Function parameters have both an external parameter name and a local parameter name. An external parameter name is used to label arguments passed to a function call. A local parameter name is used in the implementation of the function.”

This looks something like

func someFunc(externalName localName: String){
print(localName) // The localName is use for the implementation of the function

// when calling the function you use the externalName

someFunc(externalName: "Hello") 

“By default, the first parameter omits its external name, and the second and subsequent parameters use their local name as their external name. All parameters must have unique local names, but may share external parameter in common.”

func someFunc(localName: String){
print(localName) // The localName is use for the implementation of the function

// when calling the function there is no external name, since we didn’t add one.


This is just an example. The iBook goes on explaining even further.

I hope this adds to what Chris explained.

Good luck