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

Taylor Weeks
Taylor Weeks
4,009 Points

[[NSArray alloc] init]

In this video, we replace the alloc init line and replace it with an array literal (@[@"f", @"o", @"o"]). Is there a difference between these two ways to declare variables? If so, when do we use each of them?

2 Answers

Stone Preston
Stone Preston
42,016 Points

that creates an array literal. they do the same thing, its just using the literal is faster (think of it like a shorthand version). if I used

NSArray *someArray = @[@"abc", @"cde"];

versus

NSArray *someArray = [[NSArray alloc] initWithObjects:@"abc", @"cde", nil];

both accomplish the same thing. You can use either one. However the first is faster and easier. Youve been using literal syntax all along when you create a string using @"some string"

Taylor Weeks
Taylor Weeks
4,009 Points

You answered my question perfectly. Thank you.