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 trialNaomi Freeman
Treehouse Guest Teacherswift tuples?
I have no idea what it's asking me. I've been working on this for half an hour. Way too long for a question.
Challenge 1: Challenge task 1 of 3 Currently our greeting function only returns a single value. Modify it to return both the greeting and the language as a tuple. Make sure to name each item in the tuple: greeting and language. We will print them out in the next task.
Code they gave to edit:
func greeting(person: String) -> String {
let language = "English"
let greeting = "Hello \(person)"
return greeting
}
Answer:
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
Challenge 2: Create a variable named result and assign it the tuple returned from function greeting. (Note: pass the string "Tom" to the greeting function.)
Answer is so far beyond me I really don't even know where to begin at this point. Nothing I've changed does anything except throw weird errors. Pretty please help :)
9 Answers
Dino Paškvan
Courses Plus Student 44,108 PointsI think you're expecting something far more complicated than what's really being asked of you.
Just create a variable called result
under the function and store the return value of the function in it, like this:
var result = greeting("Tom")
David Coyle
2,126 PointsThanks Dino - I was working on this for over an hour, I was going far too deep into the problem making it much more complicated than it was....
Andrew Brotherton
7,515 PointsPut var result = greeting ("Tom") after the bottom curly bracket so after the function.
Anthony Boyd
1,047 PointsIn the end it should look like this for task 2
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
var result = greeting("Tom")
maxim gerashchev
1,725 PointsDid anyone find a solution? The answer presented here is not working for me :(
Eric Wood
5,194 PointsIt wasn't working for me, either. Then I noticed a subtle difference. Greeting must be before language in the tuple and in the return in order for the test to work.
(greeting: String, language: String) <i>passes the test</i>
(language: String, greeting: String) <i>does not pass the test<i/>
Adam Arul
13,757 PointsI'm stuck on the same challenge.
This is what I have so far:
func greeting(person: String,) -> (greeting: String, language: String) {
var result = greeting("Tom")
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language, result)
}
This is the error I get:
You need assign the return value of the 'greeting' function to 'result'.
Dino Paškvan
Courses Plus Student 44,108 PointsAfter you pass task 1, you're not supposed to modify the function anymore. The rest of the code is written after the greeting
function.
This is the solution for the first task:
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
For the second task, just add this:
var result = greeting("Tom")
after the function.
You are calling the greeting
function and passing the string literal "Tom" as a parameter. The function returns a tuple that you're storing in the result variable.
Naomi Freeman
Treehouse Guest Teacherlol thanks Guess I am getting overcomplicated.
To be fair, they have us working from this as the original example, and they refactored it like 4 times (and it was refactored once more beyond this below code)
func searchNames (#name: String) -> (Bool,String) {
let names = ["George", "Mary", "Leonard", "Phil", "Fluffy", "Sparkles", "Hampton", "Joy"]
var found = (false, "\(name) is not a person in this group)")
for n in names {
if n == name {
found = (true, "\(name) is a person in this group")
}
}
return found
}
let (found,description) = searchNames(name: "Sparkles")
MUZ140316 Milton Chitsike
7,344 Pointsfunc greeting(person: String) -> (greeting: String, language: String) { let language = "English" let greeting = "Hello (person)"
return (language: language, greeting: greeting)
} var result = greeting("Tom")
MUZ140316 Milton Chitsike
7,344 Pointsalways use var as variable