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

Im confused with this task

This currently passes 1 element and it ask me take make it pass 2. However the labeling of the function and the constant greeting is very confusing and not sure how to do this. I wish this was more straight forward... If you accomplish one task in the video then ask me to replicate that task in the challenge. I feel myself bouncing to other videos to get the information at hand. Please stop doing the example with a Int and then ask me to use a string in the challenge

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

    return greeting
}

1 Answer

Stone Preston
Stone Preston
42,016 Points

Task 1 states: Currently our greeting function only returns a single value. Modify it to return both the greeting and the language as a tuple. Make sure to name each item in the tuple: greeting and language. We will print them out in the next task.

so instead of returning a string, we need to return a tuple with 2 members inside it. they both need to be string and they need to be named greeting and language, in that order.

we can add a return type of tuple by placing (greeting: String, language: String) after the ->

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

    return greeting
}

however, we still need to change the return value at the end of the function to actually return this tuple. We can do this by placing (greeting, language) after the return keyword. this will return the language string and greeting string constants that get set inside the function as a tuple

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

    return (greeting, language)
}

Please stop doing the example with a Int and then ask me to use a string in the challenge

most of the challenges don't reflect exactly what was covered in the video. they require you to use what you learned in the videos to accomplish a similar, but different task. If the challenges were just an exact reflection of the video, they would not be very challenging and you would not learn as much. Much of programming is using what you have learned and applying it in different ways and situations with different types and values. It is not black and white, things will always be different.