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 trialAnna Zocher
4,721 PointsDeclaring 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?
2 Answers
Rashii Henry
16,433 Pointsyou 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.
Kai Aldag
Courses Plus Student 13,560 Pointsthey 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 :)
Hayden Taylor
5,076 PointsHayden Taylor
5,076 PointsThey 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