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

samuel gustave
samuel gustave
7,341 Points

I am returning a tuple but my code won't work var res = (greeting,language) return res

I am returning a tuple but my code won't work var res = (greeting,language) return res

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

    var res = (greeting,language)
    return res
}

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey samuel gustave,

// You need to label the components of the tuple inside your return type.

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

     // Then we modify the return statement to return these values by name
    return (greeting, language)
}

/* You then call the greeting function, by passing it a String, "Tom".
This is because it takes one argument, of type String, defined
by the function parameter. */

var result = greeting("Tom")

/* So, what happens is now result is holding the value of the function
call, greeting() on the "Tom" String. This value will be a tuple.
The first component will be greeting, the second will be language */

/* We can now access a specific component of the tuple stored
in the result variable by using dot syntax. We do this below: */

println(result.language)

Hope this helps! Good luck!