in Obj-c, when _don't_ I have to initialize an Object
February 4, 2009 2:54 PM

I initialized NSCalendarDate *now. A new NSCalendarDate iWeeksFromNow = [now dateByAdding...]. Why didn't I have to allocate and initialize iWeeksFromNow?

I'm on page 49 of the Hillegass book (3rd edition). It states:

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

I get that. *now points to an address that will hold an NSCalendar date. The memory then becomes allocated and initialized for use in the same statement.

After a few lines of unrelated code, we see:

NSCalendarDate *iWeeksFromNow;
iWeeksFromNow = [now dateByAddingYears:0 months:0 days(i*7)];


According to the API doc, dateByAddingYears.... "Returns a new calendar date that represents the date of the receiver updated with given offsets."

Does this new calendar date(iWeeksFromNow)implicitly become initialized? How am I supposed to know when I do and do not have to initialize something before using it? Does the output of an instance method always yield an initialized object?
posted by neilkod to Computers & Internet (4 answers total)
You should read this document on memory management in Objective-C, particularly this section on the primary memory management policy.
posted by strangecargo at 2:59 PM on February 4, 2009


More explanation: Your understanding of what's going on is a little wrong, primarily regarding the order in which things happen and what alloc and init do. The statement

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

is equivalent to this:

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

The first part allocates the space for the object, the second part initializes the object with default values. (An aside: the NSCalendarDate documentation doesn't list an empty initializer. Are you sure you've quoted the code correctly?) Basically, unlike many other OOP languages, Objective-C distinguishes between object allocation and initialization, instead of implicit object allocation when calling constructors, like Java/C#/C++/etc.

In your second example, the object is created and initialized within the [now dateByAddingYears:0 months:0 days(i*7)] call. If you read the primary memory management policy, you know that since you're not allocating the object yourself or receiving it from a call to a method that starts with "create", you're not responsible for memory management of the object unless you're going to keep it around past the scope of the function you're using it in.
posted by strangecargo at 3:21 PM on February 4, 2009


iWeeksFromNow is not a date— it's a pointer to a date. The only thing that dateByAddingYears:... promises is that it'll return a pointer to a date with those values. It's probably allocating it, initializing it, and autoreleasing it. But it's also perfectly valid for it to return a preëxisting NSCalendarDate if it knows of one that has the right value.
Does the output of an instance method always yield an initialized object?
Usually, yes. (strictly, a pointer to an initialized object, which will probably remain valid at least until the current autorelease pool goes away.) The language doesn't try to enforce this, though, unlike C++/Java/etc..
posted by hattifattener at 5:32 PM on February 4, 2009


@hattifattener, I totally forget that the OP may not understand pointers totally. This video is a Stanford CS classic that explains pointers. Nick Parlante is a weird dude.
posted by strangecargo at 10:46 PM on February 4, 2009


« Older Songfilter: Not the usual Tennessee Waltz   |   Explain Marx's conception of economic crises. Newer »
This thread is closed to new comments.