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 trial

iOS

Dynamic Typing Code Challenge - Objective C

Hi,

I'm stuck on the code challenge for the last part of the Objective C course. Can anyone help with this one:

Declare a variable named "thing" of type 'id' and initialize it to 'nil'.

I don't really see this part in the video and am not sure how to go about it.

http://teamtreehouse.com/library/dynamic-typing

-Stan

4 Answers

Hi Stan,

When you declare a variable, you place the type, followed by the name. To initialize it to nil, we'll just set it equal to that value, as such:

id thing = nil;

Hope this helps!

Thanks Matthew, that does help!

Seems like I'm at a total loss when it comes to this code challenge. The previous ones were okay but this one is just throwing me for a loop.

I'm now on the second part, and am stuck again.

Assign an NSNumber literal with a value of '4' to the variable 'thing'.

I've been trying different versions of the code below, but not much luck.

NSNumber thing = @4;

Because its an object literal - it uses the @ symbol prefix which will evaluate to a pointer to an NSNumber object with the stated value. In otherwords @4 will be treated as a number object without specifically stating NSNumber *thing = 4; so instead try without the NSNumber;

Thank you Justin! Finally got it :)

Why isn't it a pointer?

I initially put:

id *thing = nil;

Then my next idea was:

id *thing = @[ nil ];

Thanks for any help!

The reason you don't need id *thing = nil; is because id is an object that isn't defined until run-time or when the code is being executed. So the best way to wrap your head around it is : the star ( asterisk ) pointer - "references" the object it is pointing to in memory. Id can't be pointed to by reference because it doesn't exist yet. So when the object gets created at run-time based on your application - id will kind of transform into the object the method requires and then it will create the pointer for you. Ask yourself - "how can I point to ( refer to ) an object in memory that doesn't exist?" NSString *blabla = @"bla"; works because when the compiler see's this it knows what kind of object it is referring ( pointing to ).

I don't remember it specifying in the video ( maybe I'm wrong ) but you also don't need to use a pointer asterisk because id doesn't know which kind of object its pointing to in memory.

The above already answer the question well... but check out this link to the Apple Developer documentation for a bit of background...

https://developer.apple.com/library/ios/documentation/general/conceptual/DevPedia-CocoaCore/DynamicTyping.html