Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Advanced Objective-C Dynamic Typing

What 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
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Your 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!

Thanks 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
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Sometimes 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
ajor
8,216 Points

I just got stuck in this challenge, too. I agree with Erick that it is quite a bit confusing. Thanks for helping out, Caleb!