dateCreated in overriding init method


#1

I’m confused by a statement in the initWithItemName:valueInDollars:serialNumber: method.

From the “Initializers” section in this chapter comes “… how an instance is created: its class is sent the method alloc, which creates an instance of that class and returns a pointer to it, and then that instance is sent the method init, which gives its instance variables initial values.” My understanding is that the [[Class alloc] init] statement creates an instance of that class which would define all its instance variables and then gives them zero or nil initial values. So, I assume in main.m, after the statement “BNRitem *p = [[BNRitem alloc] init]” that all the instance variables of p are declared and defined, and I can assign values to them and use them.

Now, on to the initWithItemName:valueInDollars:serialNumber: method. Why is the statement “dateCreated = [[NSDate alloc] init];” necessary? You don’t have to include “itemName = [NSString alloc] init]”, for example. Aren’t all the instance variables of p declared after “self = [super init]?”

I feel like the answer is obvious but I can’t see it. Any help is much appreciated.

gtmcge


#2

If you want the item name initialized to an empty (default) string, then you definitely would want to use itemName = [[NSString alloc] init];

In the case of dateCreated, we do want a default date to be set, so we assign one to the instance variable. Without that initialization, dateCreated would remain nil, as per the default (though not because of anything going on with super).


#3

I guess my confusion is with the use of alloc. Let me ask you this question. With BNRitem *p = [[BNRItem alloc] init], exactly when is memory allocated for the instance variables of p, itemName, serialNumber, dateCreated and valueInDollars? My assumption is that memory is allocated in alloc and the initial zero/nil values are set in init. If that is so, why is alloc again used to allocate memory for dataCreated, in initWithItemName:valueInDollars:serialNumber:, since it should already have memory allocated for it from the alloc method in the statement BNRItem *p = [[BNRItem alloc] initWithItemName: …]?


#4

The BNRItem, after alloc, has space for all of its instance variables and those are all set to 0 (or nil) as you have correctly observed.

The itemName and serialNumber, during init, are set to point at objects that already exist. They were created in another method, passed to this initWith… method, and assigned to these instance variables. The NSDate that dateCreated points at is different because this object isn’t created until initWIth… is called. It is a brand new object that only the BNRItem knows about.

I think the big piece to understand here is that allocating a BNRItem doesn’t allocate any other objects. THe BNRItem’s instance variables can only point to OTHER objects. Those objects can be created anywhere by any object and the BNRItem must only be able to get a pointer to them in order to reference them.


#5

Thanks Joe. I have some more questions as I want to understand this process. You can PM me if that would be better, but I’ll ask the questions here.

  1. In the statement BNRItem *p = [[BNRItem alloc] init], space is allocated for a BNRItem object and its instance variables but NOT the objects to which the instance variables point, correct?
  2. You mention that the objects to which itemName and serialNumber point, are created in another method (which must be before init, since they exist when init is called). What method creates these NSString objects?
  3. Where is this method sent and to what object is it sent?
  4. Why is the NSDate object, dateCreated, not created in this method where itemName and serialNumber are created?
  5. You mention “itemName and serialNumber, during init, are set to point at objects that already exist.” Is this during the self = [super init] call?

Thanks much.


#6

I got it. I knew it was obvious and staring me in the face but I just wasn’t seeing it. The NSString objects are created in the method that sends the initWith … method to a BNRItem object. The setters then set itemName and serialNumber to the address of those NSString objects. Since an NSDate object is not a parameter to initWith … ( via something like … serialNumber: (NSString *) sNumber newDate: (NSDate *) nDate ) we must allocate memory for dateCreated and initialize it ourselves.

gtmcge