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

tuple help. no idea what i'm meant to be doing here to return two statements. any help appreciated

hh

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

    return (greeting (person:"Tom", language,"English"))
}
Josh Minor
Josh Minor
2,947 Points

You're doing good, sometimes the questions can be unclear.

Here is my solution.

//for some reason its making my arrow weird, those symbols are an arrow 

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

    return (greeting, language)
}

2 Answers

Michael Reining
Michael Reining
10,101 Points

Hi Ross,

You are very close. To return a tuple, you return two objects separated by a comma like this.

return (firstItem, secondItem)

When you apply this to the challenge it looks like this

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

    return (greeting, language)
}

While the above is a valid function for the challenge they want you to also label each of the returned values like this.

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

    return (greeting, language)
}

Tuples are great but can be very hard to grasp at first. At least I struggled with them a bit. I launched an app to help others learn Swift. There is a chapter on tuples in it that might help.

Mike

PS: Thanks to the awesome resources on Team Treehouse, I launched my first app. :-)

Code! Learn how to program with Swift

thanks to all who answered. Much appreciated. Hopefully I'll be tupling with the best of you soon.