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) Foundation Framework NSNumber

Luis Kentzler
Luis Kentzler
13,869 Points

Invalid NSNumber declaration?

I'm stuck at this code challenge, and don't seem to know why. The challenge states:

Declare another variable named 'planck' of type NSNumber and assign it the value 6.626.

My approach to this is:

NSNumber *planck = [NSNumber numberWithFloat:6.626];

I think this declares the variable planck and initializes it with a float value of 6.626. Why isn't this working?

Also, could someone explain me the difference of initializing NSNumber in the 3 different ways the video shows?:

NSNumber *planck; planck=[[NSNumber alloc] initWithFloat:6,626];
NSNumber *planck = @6.626;
NSNumber *planck = [NSNumber numberWithFloat:6.626];

Thanks a lot guys!

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Your answer is right but our CC engine is a bit restrictive so the only answer that works is: NSNumber *planck = @6.626;. All valid ways of declaring a float and they all result in the same instance variable. Neither has a benefit over the other. However, the number literal (using the @ symbol) is latest addition to Objective-C and the preferred way of creating a number.

Dave Ko
Dave Ko
11,915 Points
NSNumber *planck = [NSNumber numberWithDouble:6.626];

Give this a try for planck