Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Parameters and Tuples Tuples

Jason Zhang
Jason Zhang
1,157 Points

How to return a tuple

Hi all,

I am stuck by this question, the question asks to modify the function so that it returns tuple instead of string. But I don't get how I could do that? There's no type Tuple right? And am I suppose to return the name of the tuple of each individual elements of the tuple?

Thanks very much.

Kind Regards, Jason

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

    return greeting
}
Jason Zhang
Jason Zhang
1,157 Points

This is the original Treehouse code question:

//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.

4 Answers

Erik Uusitalo
Erik Uusitalo
8,096 Points

The "return type" for a tuple is (type, type). In this case both variables is of type String. So it should look like this...

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

And when you have told the function what it is supposed to return. You just have to put the variables in a Tuple and then return it.

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

     //Saving tuple to variable and returning it
     let t = (greeting , language)
     return t
}

Or you could write it a bit shorter like this...

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

     //Returning the tuple directly
     return (greeting , language)
}

Hope you find this helpful. Best Regards, Erik

Chris Bernardi
Chris Bernardi
3,994 Points

Erik clearly you type faster than me :)

Erik Uusitalo
Erik Uusitalo
8,096 Points

There can never be too many answers :).

Chris Bernardi
Chris Bernardi
3,994 Points

@Jason here's the answer:

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

    return (greeting, language)
}

The tuple part is what follows after the -> Here you are defining that this function returns two items both being strings (greeting & language). What this means is that later when using the function greeting(), you can access both the greeting string and language string idividually

var result = greeting("Tom") // (.0 "Hello Tom", .1 "English")
println(result.language) // "English"

Whenever I get stuck, I go back and watch the videos and redo the playgrounds. And then, I ask here :)

Jason Zhang
Jason Zhang
1,157 Points

Thanks Chris, Erik! Do the names of the tuples matter? I've changed the names to tuple1 and tuple2 for the return type(see below) and I could see that it works too

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

     //Returning the tuple directly
     return (greeting , language)
}
Erik Uusitalo
Erik Uusitalo
8,096 Points

No it doesn't mather, you can name it whatever you want.

But the name is like a "variable" so it should have a "description" of whats inside the tuple at that position, because it is this name you will use when you refer to that position like this...

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

     //Returning the tuple directly
     return (greeting , language)
}

//In you case it will look like this
result = greeting("Jason")
result.tuple1    //this refers to "greeting"
result.tuple2   //this refers to "language"   

Instead of naming them tuple1 and tuple2 you should name them something like greeting and language like this...

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

     //Returning the tuple directly
     return (greeting , language)
}

//Another way of doing it
result = greeting("Jason")
result.greeting    //this refers to "greeting"
result.language   //this refers to "language"   

Like you see this makes more sense.

Jason Zhang
Jason Zhang
1,157 Points

Got it, thanks Erik!

Erik Uusitalo
Erik Uusitalo
8,096 Points

i added some stuff at the end. And no problem!