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 Tuples

Emmanuel Ouzan
Emmanuel Ouzan
4,107 Points

I have no Idea why my code doesn't work! :( I have typed it in my Xcode playground and it works perfect!

Can someone please help me figure out what is wrong with my code?

Thanks in advance,

Emmanuel.

tuples.swift
func greeting(person : String) -> (String,String) {
    let language : String = "English"
    let greeting : String = ("Hello \(person)")

    return (greeting, language)
}

2 Answers

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Emmanuel Ouzan !

Challenge Task 1 of 3 asks you to "Make sure to name each item in the tuple: greeting and language. ", so that you have to name it like:

func greeting(person: String) -> (greeting: String, language: String) {

}

...hence the complete code should look like:

func greeting(person: String) -> (greeting: String, language: String) {
    let language = "English"
    let greeting = "Hello \(person)"

    return (greeting, language)
}
Emmanuel Ouzan
Emmanuel Ouzan
4,107 Points

Hi,

Thank you SO much for your answer.

What it means when I write "greeting" before the return data type?

It means that the return value will be name greeting and language?

Thanks,

Emmanuel.

Holger Liesegang
Holger Liesegang
50,595 Points

If you use functions with multiple return values and give them names like -> (greeting: String, language: String) you can later easily access this return values by name:

var result = greeting("Emmanuel")
println(" \(result.greeting) . You do speak \(result.language)")

For more details you might want to have a look at Functions with Multiple Return Values