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

Jonathan Scheel
Jonathan Scheel
5,531 Points

Alloc Init?

In the NSNumber video it mentions "our old friends alloc init" but to my knowledge we haven't been introduced at this stage yet. Did I skip a lesson somewhere that explains what these are and why/when they are used?

7 Answers

Stone Preston
Stone Preston
42,016 Points

those methods are used in the first video on objective C and on. thats how you instantiate an object You could have skipped that section I guess. Its the one before the section on all the main classes

Jonathan Scheel
Jonathan Scheel
5,531 Points

It's not there. The video I'm watching is http://teamtreehouse.com/library/objectivec-basics/introduction-to-objectivec/from-structs-to-objects and at 7:40 he does mention you have to "allocate it because it's using dynamic memory"... and he also mention's "we'll get into the whacky syntax shortly" but never does go into it... nor does he explain when to use alloc... or what the init is for at all. The video ends shortly there after.

The following video (https://teamtreehouse.com/library/objectivec-basics/introduction-to-objectivec/syntax-overview) does go through syntax, but again doesn't explain "alloc" or "init". It merely says they are methods that return something but does not explain what that is, why they are there, or when they are or are not used. In fact he says "something is returned but we don't need to worry about what that is... and then what is returned then has a method called on it again."

Stone Preston
Stone Preston
42,016 Points

because he creates a string literal in that case. You do not have to allocate a string literal. When you use the @"" notation they are created at compile time. A lot of objects, however, will have to be allocated and initialized

you could allocate a literal in this way though

NSString *str = [[NSString alloc] initWithString:@"literal"];

Strings are used quite a bit in programming, and it would be a pain if you had to alloc and init every single one. String literals make it easy to create an array of strings for example

NSArray *stringArray = [[NSArray alloc] initWithObjects:@"one", @"two", nil];

In the first iOS project you have to create an array of animation images and pass in file names as strings, 60 of em. Imagine having to alloc and init 60 strings.

Jonathan Scheel
Jonathan Scheel
5,531 Points

Pretty sure I watched them all... but maybe I didn't. One thing I dislike about Treehouse is if you start a video, even if it's just for a couple seconds, the site automatically marks it as being watched all the way through and skips it when you return to the section on your next session. That's probably why I'm so confused. I'll go rewatch. Thanks, Stone.

Stone Preston
Stone Preston
42,016 Points

well basically, an object has to be allocated and initialized before you can use it.

so if you wanted to create an NSArray called array you would have to write

NSArray *array = [[NSArray alloc] init];

alloc allocates the memory for the object, and init initializes its attributes. They are 2 separate processes that are usually done at the same time, one after the other.

some classes have other designated initializers such as NSArray's initWithObjects, which initializes the array with the passed in objects in it.

other languages (like java) use the new keyword to instantiate objects which looks like Classname objectName = new Classname();

Charles Massry
Charles Massry
12,253 Points

I don't remember which video but I'm pretty sure he said you have to allocate memory to a variable in one of the videos, maybe in one of the c videos.

Jonathan Scheel
Jonathan Scheel
5,531 Points

Then how does he get away with this...

NSString *sample = @"This is a test.";

Alloc and init are never called but it still works without being allocated or initialized?

This is a shorthand notation made available by the Objective-C preprocessor. Allocation and initialization are still being performed* for you behind the scene. In short, every time you see the @ symbol, it means you are given a free pass to something much more complicated/mundane.

  • This notation for NSString sometimes invoke a pattern called flyweight for certain commonly used strings, in which case allocation is skipped altogether and the same object is reused.