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 trouble figuring out what is incorrect about the following code for Q2 of the tuple challenge.

Here is the code:

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

    return reply
}

var result = greeting("Tom")

My playground seems to return the function and pass it to the variable result correctly:

https://www.dropbox.com/s/zhithjhhcbvxvkv/Screenshot%202015-02-19%2014.37.26.png?dl=0

Can you help me out?

Colin

4 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, Colin Suzuki , there's a minor problem at your greeting function

Make sure to name each item in the tuple: greeting and language

So, the tuple pair should be, greeting comes first, then language.

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

    return reply
}

That should do it

That worked! Thank you, and can move onto the next section now.

William Li
William Li
Courses Plus Student 26,868 Points

I must say that I'm a bit surprised that the part 1 grader didn't raise an error there, I'll suggest Amit Bijlani to update it.

Greg Kaleka
Greg Kaleka
39,021 Points

Hah so it took me a while to figure out what the problem was.

So, this is a little finicky, but it looks like the code challenge is looking for a tuple (greeting, language) rather than (language, greeting). Q1 accepts the function the other way around, but Q2 does not.

Thanks, that worked! I appreciate it, can finally move to the next section.

hold on, for this tuples challenge question two is asks: Create a variable named result and assign it the tuple returned from function greeting. (Note: pass the string "Tom" to the greeting function.)

The code below is what you start with. How did you get use "reply" as the var and get it to pass you to the next one? I am finding this extremely challenging to get through. I've been learning this from scratch and doing well so far until this tuples section.

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

    return (greeting, language)
}
Graham Wright
Graham Wright
1,201 Points

Hi there Marc,

The code posted above isn't correct for the challenge. You have almost everything right with your above code. you need to add a \ before person like so

let greeting = "Hello \(person)"

after that it's as simple as declaring your variable

var result = greeting("Tom")