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 trialEric Turner
1,089 PointsCode Challenge: Tuples
Not sure why, but this challenge is just not making any sense. Any direction on where to begin?
2 Answers
Meg Cusack
11,448 PointsMatt's clues helped me work this out. I was stuck on challenge 3 of 3 but this is what I finally got that passed.
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
var result = greeting("Tom")
println(result.language)
Matt West
14,545 PointsHi Eric,
This challenge starts out by asking you to change the return type of the function to use a tuple.
The tuple returned will consist of two String
items named greeting
and language
.
You can name items in a tuple by specifying the name, then a semi-colon, followed by the type. For example:
(foo: String, bar: String)
This tuple has two String
items named foo
and bar
.
Apply this concept to update the return type on the function.
Once you've done that, update the return
statement at the end of the function to return a tuple. For example:
let fooValue = "Hello"
let barValue = "Bye"
return (fooValue, barValue)
Here the first value (fooValue
) is assigned to the tuple item named foo
and the second (barValue
) to bar
.
Use this patten to update the return statement of the function and complete the first step of the code challenge.
You will then be asked to create a new variable called result
and assign it the tuple returned by the greeting
function.
All you have to do here is create a new variable using the var
keyword and then call the greeting
function to set it's value. For example:
var message = sayHello("Jo")
The final task asks you to print out a value from the tuple using the println()
function.
You can access named items within a tuple using the .
syntax. For example:
// Create a tuple
var myTuple = (foo: "Hello", bar: "Bye")
// Print an item in the tuple.
println(myTuple.foo)
The final line here would print out Hello
.
I hope this helps :)
Let me know if there's anything you're still unclear on.
Matt West
14,545 PointsMatt West
14,545 PointsGlad it helped :)