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 Objective-C Basics (Retired) Advanced Objective-C Dynamic Typing

Dynamic Typing Challenge

Just defined a variable 'thing' as an id allocated to nil. Not sure how to assign an object of a different type (NSNumber) to the same variable name.

1 Answer

Matias Hermida
Matias Hermida
1,956 Points

Amit Bijlani said:

"You cannot declare a variable with the same name twice. With id thing = nil; you are declaring that there is a new variable named thing that is initialized to nil. On the second line, once again you are declaring a new variable thing that is of type NSNumber initialized to an object containing number 4.

You can do this:

id thing = nil; thing = @4; "end of quote