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 trialAlexandra Kaplan
1,167 PointsI am on the third Tuple challenge and I know I am close
I have been just a few "typo's" shy of being correct in the last two Tuples, so perhaps someone can guide me toward what I am doing wrong with the last Tuple, without actually giving me the answer?
I am supposed to output the language "English" from the result function, so I put in:
for lang in result {println (language)}
substituting anything for the word language the first time (in this case lang). I want to figure this out, so I would appreciate a hint as opposed to an answer.
func greeting(person: String) -> (greeting:String, language:String) {
let language = "English"
let greeting = "Hello \(person)"
return (greeting, language)
}
var result = greeting ("Tom")
for lang in result {println (language)}
2 Answers
Steve Hunter
57,712 PointsHi Alexandra,
Everything there is fine except the last line.
You want to use dot notation to print out the language element of the result tuple.
Make sense?
Steve.
Steve Hunter
57,712 PointsOK. There's three things here; func greeting
, let language
and let greeting
. It is a shame that the function and one of its parameters are named the same - that just adds confusion.
This is tricky to write down ... let's start with func greeting
. This is just a piece of code that accepts two pieces of data as parameters, does something with them and sends something back to whatever asked it to do that. In your code, you've called greeting("Tom")
and assigned that to a variable called result
.
The two parameters you have passed in to the function are not related to the function in any way. They are just the materials the function uses to do its job.
Both the parameters are Strings. You can do many things to a string object but .language
isn't one of those things. The thing returned from the function when it is called is assigned to result
. That things happens to be two strings called greeting
and language
. Those two names represent their contents which are strings. To access these requires dot notation but that ability is limited to the result
variable alone.
To ask for greeting.language
has no context here. The function cannot be accessed in that way, as we know and the variable greeting has no property called language
and is unrelated to it in any way. The two varaibles have been returned by the greeting
function paired as a tuple which is stored in result
. To access them, we need to extract them from the result
variable with the dot notation as mentioned above.
I hope that makes some sense!
Steve.
Alexandra Kaplan
1,167 PointsAwesome, I get it. Thanks Steve!
Alexandra Kaplan
1,167 PointsAlexandra Kaplan
1,167 PointsAlexandra Kaplan 584 less than a minute ago I ended up looking at someone else's answered question, and saw to println(result.language). Can you please help me understand why it doesn't work when I write println(greeting.language)? Would that not too output the word "English?" Also, thank you for giving me help but not an answer.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsNo problem for giving help not an answer - I did what you asked for.
I think it is probably best for you to explain why you think
greeting.language
would produce the result you think. We can go from there!Steve.
Alexandra Kaplan
1,167 PointsAlexandra Kaplan
1,167 PointsIt seems like when you add things after a period mark (e.g. greetings.language, result.language) that you are saying "subcategory type language" of either greetings or result. Both of them have the subcategory language, so why shouldn't it work in either case?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI'll keep asking you questions rather than just answering, if that's OK? You seemed to be up for that and I think it helps if you articulate your thoughts rather than just being "told".
So, how are
greeting
andlanguage
related? Why would applying.language
to them generate the same output?Alexandra Kaplan
1,167 PointsAlexandra Kaplan
1,167 PointsI'm up for it!
Language is a subcategory of Greeting, although it also can be seen as a co-category of greeting because greeting is used in two capacities. I am working off of the assumption that the computer knows I am talking about the main category Greeting, not the subcategory Greeting.
I think if you write greeting.language, you are saying the subcategory in the greeting function of language. If you write result.language, you are saying the subcategory in the result "function" of language. However, I did notice now that the Greeting function has a generic language function (any language), while the Result function specifies English. So perhaps that's the difference?