Re: Is ther a conventional way to allocate and initialize an object in Objective-C?
Re: Is ther a conventional way to allocate and initialize an object in Objective-C?
- Subject: Re: Is ther a conventional way to allocate and initialize an object in Objective-C?
- From: "Clark Cox" <email@hidden>
- Date: Sun, 19 Aug 2007 07:44:18 -0700
On 8/19/07, Bob Ueland <email@hidden> wrote:
> I'm reading Kochan's book "Programming in Objective-C". In program 15.11 he creates an address book that is to be filled with address cards. (Address cards is a class we created earlier in the book, a simple class with just two fields name and email). I've snipped away everything except from the relevant parts in the code.
>
> ---------------- AddressBook.h, interface file ----------
> #import <Foundation/NSArray.h>
> #import "AddressCard.h"
>
> @interface AddressBook: NSObject
> {
> NSString *bookName;
> NSMutableArray *book;
> }
>
> -(AddressBook *) initWithName: (NSString *) name;
> ...
> @end
>
>
> ---------------- AddressBook.m, implementation file ----------
> #import "AddressBook.h"
> @implementation AddressBook;
>
> -(id) initWithName: (NSString *) name
> {
> self = [super init];
> if (self) {
> bookName = [[NSString alloc] initWithString: name];
> book = [[NSMutableArray alloc] init];
> }
> return self;
> }
>
> ...
> @end
>
>
> ------------ main program --------------------
> #import "AddressBook.h"
> #import <Foundation/NSAutoreleasePool.h>
>
> int main (int argc, char *argv[])
> {
> ...
> AddressBook *myBook = [AddressBook alloc];
> ...
> myBook = [myBook initWithName: @"Linda's Address Book"];
> ...
> return 0;
> }
>
>
>
> ----------------------------------------
> What I'm wondering about is the way Kochan allocates and initializes the the address book. I have two questions:
>
> 1. Is this the conventional way to initialize an object. I was expecting in the main program something like
Yes (thought the alloc, and init... are usually placed on the same
line as a single expression):
AddressBook *myBook = [[AddressBook alloc] initWithName: @"Linda's
Address Book"];
... but perhaps the author is separating them for a reason (maybe to
illustrate that allocation and initialization are two separate steps
in Cocoa).
> AddressBook *myBook = [[AddressBook alloc] init];
> NSString *addressBookName = @"Linda's Address Book";
> [AddressBook setName:addressBookName];
> [AddressBook setBook];
>
>
> where setName and setBook would be used insted of initWithName.
If a class' instances are often initialized with a particular set of
values, you will frequently find an init... method to simplify things
for you.
>
> 2. Why does Kochan use the test
> if (self) {
>
> in the initWithName method. I mean if self does not exist the method initWithName would never be called, would it?
If self was nil *before* initWithName: was called, then you are
correct; initWithName: would never be executed. However, you're
forgetting that there is the possibility that [super init] will return
nil (to indicate a failure of some kind), in which case attempting to
access the bookName and book would be a bad thing (as it would involve
dereferencing a NULL pointer).
--
Clark S. Cox III
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden