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 am stumped on the first task in this challenge how do I convert the strings in this function to a tuple?

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

return greeting

}

I understand how the tuple works within his example but I am unable to apply it to the challenge.

7 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Spencer. I presume you are stuck in the 1/3 code challenge in the tuples section.

It should go like this: 1) before the curly braces we define what we are going to get, so we need to add: (language: String, greeting: String) 2) We need to change the return statement, from just return greeting to return language, greeting.

So final code would be:

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

    return (greeting,language)
}

I hope this makes it clear

Jaemin Yi
Jaemin Yi
4,040 Points

Can someone explain why the test passes if it's (greeting,language) but not if it's (language,greeting)?

I don't understand why the order would matter here, or why someone would change it from (language,greeting) in the first place. Can someone explain? Thank you!

Hello Vittorio! Thank you for your answer. But i still can not understand my mistake. Here is my code.

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

It works fine in playground on my Mac, but i still get an error here. :( I did not forget to put backslash before (person) for interpolation.

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Vladimir.

It looks like for some reason in this last code you have provided you inverted the returned values (which you did not do in the previous pasted code).

If you swap them, greeting first then language you should pass it.

Could you also help with the second task. I can not understand what does task want me to do. Look at my code:

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

return (greeting,language)

} var result = greeting(person:"Tom")

I declared a variable and asigned it to the tuple which returns after function works. But system still writes to me "Your result variable has the wrong value in it. Check the task instructions again." What is wrong?

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Vladimir Golikov .

I noticed an error in your code which is: "greeting = "Hello (person)". This should be instead "Hello (person)"" as we need String interpolation here.

The rest of the code looks ok and it should work after that correction.

I haven't used the # for this exercise, but if you then specify "person:" when calling the function it should work good too.

;)

Vittorio, thank you so much for help! Now its ok!

i stuck in third question, here is my code for print the language, but i still get an error

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

    return (greeting,language)
}

var result = (greeting: "Tom",language: "English")

println("\(result.language)")

any help?

println(result.1)

Check out around the 6:17 mark in the video on Tuples.

Thanks sherrie, works like a charm :D

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Christopher.

It would pass 1/3 in both cases. The order does not really matter for the first code challenge.

It matters for the 2/3 instead as we return a tuple, so more than 1 value basically and we want them to be in the right order inside this tuple.

This is the reason why returning (greeting,language) or (language,greeting) makes a difference.

Vittorio

thanks Vittoria Somaschini , i really found an important information in your answers thank you :) you are Talented