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

First Tuple Challenge

I'm having trouble in Task #2.

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

I keep getting the following error:

"use of unresolved identifier 'language' var result = (greeting,language)"

Can anyone please help?

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

    return (greeting,language)
}
var result = (greeting,language)
greeting("Tom")

3 Answers

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

so you need need to assign result the return value of calling the greeting function and passing in "Tom" as an argument:

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

    return (greeting,language)
}
//assign result the return value of calling the greeting function
var result = greeting("Tom")

Thanks Stone Preston!

So it looks like I just had a basic syntax error.

It appears when assigning a tuple to a variable, I do not place the tuple inside parentheses (), correct? Or is that only in this instance that I do not place it inside parentheses?

I get confused because in the video Amit placed parentheses around the tuple assigned to the variable "found".

Why is answer to his example and the task so different?

func searchNames (#name: String) -> (Bool,String) {
    let names = ["Amit","Andrew","Ben","Craig","Dave","Guil","Hampton","Jason","Joy","Kenneth","Nick"]

    var found = (false,"\(name) is not a Treehouse Teacher")

So it looks like I just had a basic syntax error.

not exactly. you did this:

var result = (greeting,language)

what that did was attempt to assign a tuple to result, but you used greeting and language as the values of the tuple and those are not available outside the scope of your greeting function.

what you were tasked with doing is Create a variable named result and assign it the tuple returned from function greeting

you need to assign result the return value of a function call, not assign it a tuple directly. the correct code is:

var result = greeting("Tom") 

which assigns the return value of calling the greeting function to the variable result. the return value of calling the greeing function is a tuple.

greeting("Tom") 

is not a tuple, it is a function call.

It appears when assigning a tuple to a variable, I do not place the tuple inside parentheses (), correct? Or is that only in this instance that I do not place it inside parentheses?

no you do place it inside parenthesis when creating a tuple. if you had a variable named friend and wanted its value to be a tuple with the members firstName and lastName you would do:

var friend = (firstname: "stone", lastName: "preston")

you enclose the tuple in parenthesis.

Thanks again Stone Preston.

I'm having trouble comprehending this - which indicates that I need to become more educated on the meanings of all these new words (results, calls, values, etc.). I think that's where I'm getting confused.

I really do appreciate your help.

you may want to go back through the videos a second time. thats usually what I do if I find out im getting lost.