1

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!

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Josh. Was in a hurry.
4

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.

1 Comment

Thank you for the detailed explanation. The tutorial "Objective-C Programming: The Big Nerd Ranch Guide" starts of using the shothand method NSDate *now = [NSDate date]; and then switches to the method using alloc. The example was in a section describing nested sends so perhaps that is why it was used. I was confused because the first method seemed much more simple to use.
3

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).

3 Comments

Basically, [NSDate date] is a wrapper that uses alloc init internally.
@John_Woods, Does [NSObject new] return an autoreleased instance? From my understanding of the rules it does, just need to confirm this ?
@pnizzle, Yes that's right, obviously with ARC it doesn't really matter though!
0

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.

2 Comments

It is only not necessary in ARC. Under manual retain release, there is a big difference...
@bbum If the tutorial he mentions says that the transition from autoreleased object to owned is necessary, than the tutorial is wrong. Is there a reason for that? Also, I assume everyone uses ARC today. Especially beginners should really do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.