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

I'm having difficulty completing Tuple Challenge 2 of 3. Do you have the answers?

I wrote this:

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

return (language, greeting)

} var result = greeting("Tom")

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

    return (language, greeting)
}
var result = greeting("Tom")

4 Answers

Chris Foreman
Chris Foreman
1,540 Points

Hi, Your code is right...I was stuck on this challenge for a while, but found that the Treehouse compiler wants the return values to be in a certain order. If you simply flip your values where "greeting" is the first one and "language" is the second one then it will work.

Thanks, Chris. You're right. In Xcode it works in any order but the Treehouse complier just accepts a certain order.

Carlos Freire
Carlos Freire
2,289 Points

Hello,

I just tried this and its not working. It says "Your result variable has the wrong value in it"

Any ideas? I have the same code as Paul above.

Nick Jones
Nick Jones
2,086 Points

Hi Paul

You're so close! With tuples as two objects are stored within them you need to explicit say which of the two you want to assign to your new variable. What you have currently:

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

    return (language, greeting)
}
var result = greeting("Tom")

Is very close but you're missing one key part which is where you are setting the value of your new variable "greeting". What will happen with the code above is result will equal the results of both objects within your tuple so would look something like: (English, Hello Tom)

As it will return the language object along with the greeting. To cut a long story short the remedy for this is:

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

    return (language, greeting)
}
var result = greeting("Tom").greeting

Note the dot notation and then the explicit deceleration of 'greeting' after passing "Tom" in to your greeting function on the last line, with tuples this is how you get out of them the object that you want rather than all of the objects at once.

Hope this helps, if not just let me know.

Thanks for your help Nick. Your code works perfectly in Xcode but when I put it in the Treehouse compiler and check answer it throws up the error message 'Something's not right. You need to assign the return value of the 'greeting' function to 'result'.' Am I missing something somewhere?

Andrew Condon
Andrew Condon
699 Points

Got it to work but didn't need the .greeting at the end of the result variable

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

    return (greeting, language)
}
var result = greeting("Tom")
Carlos Freire
Carlos Freire
2,289 Points

Hello,

I tried all of these and none of them work.

Any ideas?