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 Decomposing a Tuple

I don't understand the relationship: result.language

So, this was written for the Challange at the end of Tuples.

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

    return (greeting, language)
}

var result = greeting("Tom")

print(result.language)

I don't understand how the variable "result" is related to the language String.

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Cody Iddings,

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

    return (greeting, language)
}

var result = greeting("Tom") 

// here you are creating an instance of your greeting method, 
// and assigning the value to the var "result"

print(result.language)

// here you are accessing the value of language in that instance and printing the value

Good luck!

I think I understand what you are saying. The instance I'm creating is wrapped up into the variable "result" and so now accessing value "language" through the variable "result"

thanks Steven!

Pedro Laplaza
Pedro Laplaza
1,960 Points

Hi Cody, i had kind of the same issue, it was a little confusing for me to understand where i was pointing when i named each thing so i created the same thing with completely different names to clarify and works, might help you to understand where the values are taken from

func greeting(person: String) -> (greetingString : String,theLanguage : String ){
    let theStringForLanguage = "English"
    let theStringThatSaysHello = "Hello \(person)"

    return (theStringThatSaysHello ,theStringForLanguage)
}

var result = greeting("Tom")
print(result.theLanguage)