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

Alex Chiang
Alex Chiang
2,222 Points

What is the difference between a literal assignment compared to using alloc, init?

From the lesson, we learn we can assign NSNumber objects in 2 ways:

NSNumber *value = @25;

or

NSNumber *value = [[NSNumber alloc] initWithInt 25];

What is the difference between this two? Which one is used more often?

1 Answer

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

They are the same exact thing, the only difference is

NSNumber *value = @25;

is shorter and easier to write then

NSNumber *value = [[NSNumber alloc] initWithInt 25];

It appears that the second one is used more often although I'm not sure why.

Alex Chiang
Alex Chiang
2,222 Points

Thank you Caleb! I really appreciate your help.