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 trialTrue Pixels
3,178 PointsI'm stuck on Stage 2 "Parameters and Tuples"
The question was: Using the println statement, print out the value of the language element from the result tuple.
And when i did this code, it kept on saying an error message which said:
"invalid redeclaration of 'result'" & "Use of unresolved identifier 'language'"
I have rewatched the video, but i couldn't find anything about this, so i tried doing what i could possibly think of but none of that worked, so if someone could help me that would be great :)
func greeting(person: String) -> (greeting: String, language: String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
//only pass a person to the function
var result = greeting("Tom")
println("language = \(language)")
3 Answers
Stone Preston
42,016 Pointsthe swift eBook states:
If you name the elements in a tuple, you can use the element names to access the values of those elements:
and provides the following example:
let http200Status = (statusCode: 200, description: "OK")
println("The status code is \(http200Status.statusCode)")
so you use dot syntax to access named tuple elements
tupleName.elementName
so to access the language element you would use
println(result.language)
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)
dalal kh
iOS Development Techdegree Student 2,489 Pointsprintln (result.lanuage)
True Pixels
3,178 PointsOh, i thought i tried that, i guess i forgot the add the "()"'s
Gilroy Timoty Graham
Courses Plus Student 1,916 PointsGilroy Timoty Graham
Courses Plus Student 1,916 Pointshi, i didn't understand this:
println(result.language)
why its result.language not greeting.language, because the func was greeting()?
thank you ;)