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 NSArray

Which of these styles is more commonly used?

After going through a few of the Foundation Framework objects, there are two ways to write the values for objects. Example:

First Method

NSArray *foo = [[NSArray alloc] initWithObjects: @"f", @"o", @"o", nil];

Second Method

NSArray *foo = @[@"f", @"o", @"o"];

I would much more prefer to use the second method when writing code because it is a lot easier and faster to write, but would there be any downside to only using this method? Is there a time where the first method (alloc, init) would be better to use?

1 Answer

Stone Preston
Stone Preston
42,016 Points

I use literals a lot more because its faster. if there is a literal way I use it because its a lot faster than having to call alloc and init all the time

So, by using a literal, it basically allocates the memory and initializes objects automatically without the need to type it all out?

Stone Preston
Stone Preston
42,016 Points

yep thats exactly what it does.

Thank you!

Stone Preston
Stone Preston
42,016 Points

no problem, you can read all about literals here