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

Richard Perry
PLUS
Richard Perry
Courses Plus Student 876 Points

Task 2 of 3, Tuples, what is the challenge code the exercise is looking for?

I am trying to work through the challenges under the SWIFT track, Function and Options, Tuples. The code I am creating is working fine when I create it under xcode 7.1 and it is returning the requirements of the challenge. The challenge appears to be seeking something specific. I am getting a bit frustrated with being unable to proceed on these exercises because I can't move past challenges. There is no way to skip a challenge or proceed if the code is not exactly what the challenge is seeking. Presently I am stuck and cannot proceed on this track because this challenge won't get resolved.

In any case, please provide the location on you site where I can obtain the code "answers" to all of your challenges, so that I may move on when this problem occurs.

For this particular problem here is the code placed in the challenge.

This code answer the challenge and works fine.

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

return greeting

} var result = greeting("TOM")

Specifically in your answer, you just forgot to add the \ to the string interpolation for "person", however, there are more things the challenge wanted detailed in the answer below.

2 Answers

Task 1: Modify the greeting function to return both the greeting and language as a tuple. Remember to return inside the function (the "return") and in the declaration (after the "->")

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

    return (greeting, language)
}

Task 2: Create a variable named result and assign it to greeting. Pass the string "Tom" to greeting.

var result = greeting("Tom")

Task 3: Use the print function, print the value of the language element from the result tuple. This is just using dot notation to access the value.

println(result.language)
Nick Kohrn
Nick Kohrn
36,935 Points

Hello Richard,

The code below passes the 2nd task of the challenge:

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

        return (greeting, language)
    }

    var result = greeting("Tom")

I suspect that the fix is to pass Tom to the greeting: function rather than TOM as an uppercase string as you have typed it.

I hope this helps!