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 Build a Simple iPhone App (iOS7) Creating a Data Collection Implementing an Array

Declaring Array: When to use literals versus alloc + init

In this class, we declare an array using alloc and init. I understand that this puts memory allocation in our control, and the memory will be held for the array until we release it.

Because we are using this array the entire time the app is open, why not use a literal declaration instead:

NSArray *prediction = @[@"prediction1", @"prediction2", etc];

When do you use a literal declaration, versus allocating memory yourself?

Hayden Taylor
Hayden Taylor
5,076 Points

They want to show you the long way so its more readable for new users.....

When you develop your own stuff you will most likely use literals because they are way better

2 Answers

Rashii Henry
Rashii Henry
16,433 Points

you can use either and end up being fine in both scenarios. However, if you want to do everything in one line and avoid typing more lines of code then use your literals. Its really a personal preference but they both have the same outcome.

they both get the job done, personally littorals get it done quicker and it's more readable but they both get the same outcome so it's all about your preference :)