4

Why do some objects not need to be initialized before use in objective-c? For example why is this NSDate *today = [NSDate date]; legal?

Hagelin
  • 16,440
  • 5
  • 29
  • 37
lampShade
  • 4,361
  • 8
  • 34
  • 50
  • See also this recent question: http://stackoverflow.com/questions/5987969/objective-c-self-allocating-objects/5988016 – jscs May 13 '11 at 17:35

4 Answers4

4

They are initialized within the date method. This is a common way to create autoreleased objects in Objective-C. Allocators of that form are called convenience allocators.

To learn more about that, read the "Factory Methods" paragraph in Apple's Cocoa Core Competencies document about Object Creation: http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/ObjectCreation.html

To create convenience allocator for you own classes, implement a class method, named after your class (without prefix). e.g.:

@implementation MYThing
...

+ (id)thing
{
  return [[[MYThing alloc] init] autorelease];
}

...
@end
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • 1
    Basically you can find convenience allocators by looking at the method name and the '+' sign in the method declaration. The name often is the class name without prefix, and the plus sign tells you that it is a class method. Apple provides them for a lot of their classes. Some prominent examples:[NSString stringWithFormat:], [NSNumber numberWithInt:], ... – Thomas Zoechling May 13 '11 at 13:26
  • 2
    The convention is that methods whose names do not begin with “alloc”, “new”, “copy”, or “mutableCopy” return autoreleased objects – albertamg May 13 '11 at 13:28
  • A small nitpick: they are "convenience _constructors_" not "allocators". See ["Combining Allocation and Initialization"](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAllocInit.html#//apple_ref/doc/uid/TP30001163-CH22-SW12), and my recent answer to: ["Self allocating objects"](http://stackoverflow.com/questions/5987969/objective-c-self-allocating-objects/5988016). – jscs May 13 '11 at 17:37
  • I read the term "convenience allocator" in "Cocoa Programming" by Anguish et. al. http://books.google.com/books?id=AeyX8GqdzPYC&pg=PA153&dq=%22convenience+allocator%22&hl=en&ei=o3PNTZ29PIiF-wbIq92QDA&sa=X&oi=book_result&ct=result&resnum=2&ved=0CC8Q6AEwAQ#v=onepage&q=%22convenience%20allocator%22&f=false. But "convenience constructor" makes sense of course. – Thomas Zoechling May 13 '11 at 18:11
  • The only reason, I believe, that "constructor" is more correct is that such a method doesn't _necessarily_ allocate memory, as in the case of a singleton. Like I said, though, it's just a small nit, and only based on "this is what Apple says". – jscs May 13 '11 at 20:27
1

today is initialized (and autoreleased) inside the static date call.

Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
0

You only need to called an init… method on objects you have allocated by calling alloc. alloc only reserves space needed for the object, creating a an unitialized object.

An uninitialized object have all instance variables set to zero, nil, or equivalent for the type. Except for the retain count that is set to 1.

All other methods that return an object are guaranteed to return a fully initialized object. alloc is the exception.

You must never call an init… method on an object that is already initialized. Simple rule on thumb is to use a 1-to-1 relation between alloc-init…, thats it.

PeyloW
  • 36,742
  • 12
  • 80
  • 99
0

Two parts.

First, as others have mentioned, a method can initialise and then autorelease an object before returning it. That's part of what's happening here.

The other part is how it's defined. Note how most Objective C definitions begin with a -? The one you mention does not. The signature looks like this:

+ (NSDate*) date;

That is, it's a class method and applies to the class as a whole rather than to an instance of that class.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152