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

Ryan Anderson
Ryan Anderson
4,610 Points

Having a hard time understanding Challenge Task 2 of 3. Could someone please break this down for me? thanks

.

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

    return (greeting, language)
}

var result =

3 Answers

Brendan,

You have returned your tuple in the wrong order, I think. That is why your result variable fails the test for this challenge

The return statement should be return (greeting, language)

Steve.

Hi Ryn,

Let's start with the solution to part 2:

var result = greeting("Tom")

So, in the previous part, you amended a method to return a tuple by changing it's method signature (the first bit of the method) and also the method body; the return statement.

The next part asks you to assign the result of the method into a variable called result. As above, we declare a variable, call it result and then use the method that you've amended by passing in the required parameter "Tom". This method returns a tuple so that is now stored in the variable result.

I hope that makes sense!

Steve.

Brendan O'Brien
Brendan O'Brien
5,521 Points

Hi Steve:

Tried that but it fails:

Error message: Bummer! Your result variable has the wrong value in it. Check the task instructions again.

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

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

  return (language, greeting)

}
var result = greeting("Tom")
Brendan O'Brien
Brendan O'Brien
5,521 Points

Boy did I tear my hair out over that for a couple hours...

Steve, you were absolutely correct. Re-ordering the return statement did the trick.

On a more general note, does the order matter in real code?

This worked:

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

  return (greeting, language)

}
var result = greeting("Tom")

Absolutely the order matters in real code. Your tuple could contain the results of two separate calculations, say, holding the speed and direction of a particle. If you were expecting the speed to be contained in tuple.0 and the direction to be found in tuple.1 but these two results were reversed, your calculation is likely to be incorrect.

Similarly, in our little example in this code challenge, if your application is expecting to discover the user's language by accessing result.1 but instead found that contained "Hello Tom", that's likely to cause unforseen results.