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

Correct tuple in result, player throwing an error that my "result" is incorrect

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

    return (language, greeting)
}

var result = greeting("Tom")

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Carl,

You simply have your tuple backwards, greeting should come first then language.

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

    return (greeting, language)
}

Is there any reason why that matters? I thought the pointed of named variables is that they're accessible by name instead of location.

'''swift
var result = greeting("Tom")
'''

will still yield

'''swift result.greeting // "Hello Tom" result.language // "English" '''

Holger Liesegang
Holger Liesegang
50,595 Points

You could access "language" and "greeting" via the index notation as follows:

var result = greeting.0
var result = greeting.1

...in this case the order of the variables in the return statement would matter but IMHO you're right that the challenge should accept your answer anyway :)

Chris Shaw
Chris Shaw
26,676 Points

Hi Carl Reyes,

Is there any reason why that matters? I thought the pointed of named variables is that they're accessible by name instead of location.

For this challenge the order does matter as it's explicitly stated in the task question, however for real life purposes if you have named tuples you don't need to worry about the order since you can access the values simply using their name. See the below link as a I wrote in a bit of detail the different ways you can use tuples.

https://teamtreehouse.com/forum/not-sure-on-the-tuple-question-ios-swift-to-modify-the-greeting-to-return-both-language-and-greeting

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Carl,

everything looks fine but just try changing the order of "language" and "greeting" in your tuple like this and it'll work:

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

    return (greeting, language)
}

var result = greeting("Tom")