Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Parameters and Tuples Tuples

becky hayes
becky hayes
2,226 Points

Not sure how to assign a variable to a tuple

Having real problems here ...i've created a variable... but im not sure how to assign a variable to a tuple

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

    return (language, greeting)
}

3 Answers

jonlunsford
jonlunsford
10,899 Points

Becky: You are on the right track. Here's how I would implement the function.

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

Then you can create a variable called myGreeting and store the return value from the function. In this case a tuple. Using tuple notation you can print the greeting using myGreeting.greeting. To print the language use myGreeting.language. Here's an example...

var myGreeting = greeting("becky")
print(myGreeting.greeting)
becky hayes
becky hayes
2,226 Points

Thanks for this! I still cant get past the challenge.. its asking me to:

Create a variable named result and assign it the tuple returned from function greeting. (Note: pass the string "Tom" to the greeting function.)

currently my code looks like this but i dont know what i'm doing wrong!

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

return (language, greeting, result)

}

jonlunsford
jonlunsford
10,899 Points

Outside your function you would declare the variable result. Not in the return statement as you have it above.

func greeting () {
...
}

var result = greeting("your name here")

You are assigning what is returned from the function to a new variable named result. By calling the function, the return value will be stored to result. I hope this helps!