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

asher Fischaum
asher Fischaum
2,756 Points

task 1 of test after tuples

I am stuck. i do not fully understand what the task is asking me to do and how exactly to do it. could someone please help me out?

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

    return greeting
}
Jhoan Arango
Jhoan Arango
14,575 Points

Hey Asher, I would like to help you, but I don’t want to give you the answer straight up without first knowing what’s the part that is confusing you. If you can, please post the question they are asking you or the part you don’t understand, so that I can help you and explain with details.

2 Answers

You have two constants inside function. You need to return both of them as tuples. This is the answer

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

    return (greeting, language)
}
asher Fischaum
asher Fischaum
2,756 Points

Hi Peter, thank you for your speedy reply.

how comes it would not be written:

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

    return (greeting, language)
}

This is the error i get:

"Your function needs to return a tuple with elements named 'greeting' and 'language'."

Hey Asher, you are welcome. I am happy to help ;)

Let me try to explain:

Task says "Your function needs to return a tuple with elements named 'greeting' and 'language'."

When we want to say to a function what needs to be returned we use this -> (minus and greater than sign).

So in your code you say -> (String, String) which means return two strings, but what you actually need to say is return me greeting and language and they need to be Strings, so it's go like this -> (greeting: String, language: String)

I hope you understand now.

Just keep learning and practice and after some time you will be amazing developer. Don't be discouraged when you can't find solution, even best developers can't always figure out where is the problem.

Happy coding

Jhoan Arango
Jhoan Arango
14,575 Points

This is your code:

          // You have 2 parameters, and a tuple return of type strings. 
func greeting (person: string, language: string) -> (String, String) {
    let language = "English"
    let greeting = "Hello \(person)"

    return (greeting, language)
}

This is how it should be

         // One parameter, and a tuple with 2 named returns of type string. 
func greeting(person: String) -> (greeting: String, language: String) {
    let language = "English"
    let greeting = "Hello \(person)"

    return (greeting, language)
}