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 trialCarl Reyes
Courses Plus Student 7,225 PointsCorrect tuple in result, player throwing an error that my "result" is incorrect
func greeting(person: String) -> (language: String, greeting: String) {
let language = "English"
let greeting = "Hello \(person)"
return (language, greeting)
}
var result = greeting("Tom")
2 Answers
Chris Shaw
26,676 PointsHi Carl,
You simply have your tuple backwards, greeting
should come first then language
.
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
Holger Liesegang
50,595 PointsHi Carl,
everything looks fine but just try changing the order of "language" and "greeting" in your tuple like this and it'll work:
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
var result = greeting("Tom")
Carl Reyes
Courses Plus Student 7,225 PointsCarl Reyes
Courses Plus Student 7,225 PointsIs there any reason why that matters? I thought the pointed of named variables is that they're accessible by name instead of location.
will still yield
'''swift result.greeting // "Hello Tom" result.language // "English" '''
Holger Liesegang
50,595 PointsHolger Liesegang
50,595 PointsYou could access "language" and "greeting" via the index notation as follows:
...in this case the order of the variables in the return statement would matter but IMHO you're right that the challenge should accept your answer anyway :)
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsHi Carl Reyes,
For this challenge the order does matter as it's explicitly stated in the task question, however for real life purposes if you have named tuples you don't need to worry about the order since you can access the values simply using their name. See the below link as a I wrote in a bit of detail the different ways you can use tuples.
https://teamtreehouse.com/forum/not-sure-on-the-tuple-question-ios-swift-to-modify-the-greeting-to-return-both-language-and-greeting