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 trialBrennan Robinson
790 PointsAfter testing the question that begins "NSString *primaryLanguage;" question, the current correct answer is wrong
I tested this in Xcode:
NSString *primaryLanguage;
enum country {
USA = 0,
UK = 1,
Germany = 2,
Thailand = 3,
};
int country = UK;
switch (country) {
case USA:
primaryLanguage = @"American English";
NSLog(@"The primary language is American English.");
break;
case UK:
primaryLanguage = @"English";
NSLog(@"The primary language is British English");
break;
case Germany:
primaryLanguage = @"German";
NSLog(@"The primary language is German.");
break;
case Thailand:
primaryLanguage = @"Thai";
NSLog(@"The primary language is Thai.");
break;
default:
primaryLanguage = @"Pig Latin";
NSLog(@"The primary language is Pig Latin");
break;
}
The result is British English in my NSLog. The difference: I set "country" as an int, which in the prior code, it was not even declared as a variable, and was used in the switch. That was incorrect. So with this modification, the result is switch UK, not switch Germany.
2 Answers
Preston Skaggs
11,818 PointsFirst of all your method is wrong. You've defined country
as an enum
and then defined it as an int
. Next, you have not replicated what was being defined in the question. The question tells you to check the syntax and is hinting at the fact that there are missing break statements in the first two case statements. That is why the answer is not "English".
Preston Skaggs
11,818 PointsWhat is wrong is using the same variable name in the same scope for two different definitions. So yes your method was wrong because of that. I agree that it is not wrong to use a variable to hold a value.
Brennan Robinson
790 PointsBrennan Robinson
790 PointsThanks for the clarification, because I definitely overlooked that.
Preston Skaggs
11,818 PointsPreston Skaggs
11,818 PointsYou're welcome. I do that type of stuff all the time. It helps to have another set of eyes take a look.
Brennan Robinson
790 PointsBrennan Robinson
790 PointsOne thing though, it is not "wrong" to define a variable called country to store this data, the only mistake would be to name it the same name as the enum. So I should have renamed it to targetCountry, or some more precise name. Clarity in the code, rather than assigning the enum to the target value is a better coding practice. (If you don't believe me, check out Apple's docs, MSDN (for C language specifications), and stack overflow.