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 trialerickwatson
1,783 PointsWhat is different in the syntax for assigning literals to 'id' as opposed to static variable types? Apart from no '*'?
The challenge is to assign an NSNumber literal '4' to the id 'thing'. Nowhere in this lesson is treating id like other variables/pointers really discussed... only shown how it is used in a for loop. So my assumption is one would use the same syntax to assign it a literal that you would if you weren't using id.
id thing = [NSNumber:@4];
And in fact I find examples of this outside the Treehouse environment. But this doesn't work in the code editor for the challenge. The lesson doesn't cover the more basic usage of 'id' at all. Even the first question, (assign 'nil' to id 'thing) I simply guessed at. And it DID act like other variables/pointers.
4 Answers
Caleb Kleveter
Treehouse Moderator 37,862 PointsYour right, it is confusing, when I did the challenge I was stuck on it for over a week. So here is the answer:
id thing = nil;
NSNumber *four = @4;
thing = four;
NSLog(@"thing = %@", thing);
I hope this works!
erickwatson
1,783 PointsThanks for quick reply. So does that mean MY version was literally wrong? Or it just wasn't the one the challenge editor was looking for? Most times I've found the challenge editor to be pretty flexible on accepting more than one way of doing something...
Caleb Kleveter
Treehouse Moderator 37,862 PointsSometimes what happens is you put in code that passes one stage but it makes it impossible to do the other tasks. Another mistake that can be made that it looks like you did was that you have to keep the previous code from other stages and not change it, so if the code challenge says to modify code you already wrote then you keep the previous code and then write the modified code under it. And BTW, the code for the challenge that I gave you, line 1 is for the first task, lines 2, and 3 are for the second task and line 4 is for the last task.
ajor
8,216 PointsI just got stuck in this challenge, too. I agree with Erick that it is quite a bit confusing. Thanks for helping out, Caleb!
Caleb Kleveter
Treehouse Moderator 37,862 PointsNo problem!