Question

I have been going over some tutorials in Objective-C and I am confused when it comes to the command alloc() and how it is used to initialize an object.

The tutorial says to do the following and I am having trouble figuring out why it is necessary.

Change

NSDate *now = [NSDate date];

to

NSDate *now = [[NSDate alloc] init];

Why is this change necessary since the first line works just fine? I guess I am looking for how the two lines differ. Thanks!

Was it helpful?

Solution

There's a simple difference and a deeper one.

The simple answer is that the first method includes an +alloc/-init pair internally—the documentation tells us that it returns a new date object initialized to the current time. Generally, somewhere, somebody has to call +alloc and an -init method of some sort. Sometimes that's you, sometimes a convenience method has been included for you.

The deeper answer about the difference is that +alloc/-init returns an object that is owned by the caller, who is then responsible for calling -release at some point, while convenience constructors that don't start with the words "alloc" or "new" return autoreleased objects that you don't have to release. However, if you are using ARC, this is mostly academic, as the compiler tracks that detail for you.

OTHER TIPS

Break it down:

NSDate is a class. So NSDate alloc is a call to the class method alloc. That's actually inherited from NSObject and acts to create sufficient storage for a new instance of NSDate, then return it for use as an instance.

(instance) init is a call to the instance method init. Prior to calling init the instance you've received is not guaranteed to be in a valid state. Calling init or the relevant initialiser gives the instance the chance to establish itself.

NSDate also choses to supply the class method date. That does the same thing as [[[NSDate alloc] init] autorelease] and is provided as a mere shorthand.

As other posters have commented, there is a semantic difference here — alloc returns an owning reference. So it's the caller's responsibility to release the object later. date returns a non-owning reference. So the caller has no responsibilities. However the modern ARC compiler will deal with releasing things for you. So there's a difference but not one that has any real effect on you.

If your tutorial insists that date is more proper then either it was written before the ARC compiler or was written by someone that prefers to use the old conventions; using date would traditionally communicate that the thing you were creating was for transient use so there is arguably some extra value in the one way versus the other for an experienced developer.

With ARC it doesn't matter as you no longer have to release objects you alloc init.

Without ARC the difference in important:

[NSDate date] 

returns an autoreleased object using a class method on NSDate.

[[NSDate alloc] init] 

returns a non autoreleased object instance, with a retain count of one.

As in non- ARC, you need to take memory management in your own hands, So, alloc init is better option. So, that you can release it, once not required.

One further point to note, is that autoreleased objects are released when the autorelease pool is.

When you alloc init you know that your object will stay around until you release it (or you leak it because it goes out of scope).

The change is not necessary at all. It doesn't matter if you create object using alloc+init or using convenience methods. Think about them as legacy and convenience methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top