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

Gys Hyman
Gys Hyman
2,838 Points

Error when assigning the tuple returned from a function.

The code challenge is "Create a variable named result and assign it the tuple returned from function greeting. (Note: pass the string "Tom" to the greeting function.)" but I am getting an error, "your result does not contain the correct value".

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

    return (language, greeting)
}

var result = greeting("Tom")

4 Answers

Michael Hulet
Michael Hulet
47,913 Points

In the declaration of your function, you promised a named tuple, but you're not actually returning one. Edit your function to look something like this:

func greeting(person: String) -> (language: String, greeting: String) {//<----
    let language = "English"                                               //|
    let greeting = "Hello \(person)"                                       //|
    //Notice how I specify the name of each element of the tuple, like above |
    return (language: language, greeting: greeting)
}

var result = greeting("Tom")
Gys Hyman
Gys Hyman
2,838 Points

Thank you for your response. It works in Playground, but Code Challenge still giving an error. "Bummer! Your result variable has the wrong value in it. Check the task instructions again."

Challenge Task 2 of 3

Create a variable named result and assign it the tuple returned from function greeting. (Note: pass the string "Tom" to the greeting function.)

Michael Hulet
Michael Hulet
47,913 Points

One last idea:

func greeting(person: String) -> (language: String, greeting: String) {//<----
    let language = "English"                                               //|
    let greeting = "Hello \(person)"                                       //|
    //Notice how I specify the name of each element of the tuple, like above |
    return (language: language, greeting: greeting)
}
//You might have to name the parameter you pass in, like below
var result = greeting(person: "Tom")
Gys Hyman
Gys Hyman
2,838 Points

Thank you, the Playground is a accepting the result, but the Code Challenge is still rejecting it.

Gys Hyman
Gys Hyman
2,838 Points

Resolved. Correct answer below.

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

return (greeting: greeting, language: language)

}

var result = greeting("Tom")