• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: semantics of init
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: semantics of init


  • Subject: Re: semantics of init
  • From: Marcel Weiher <email@hidden>
  • Date: Wed, 4 Jun 2003 10:09:03 +0200

If you want to be even safer, you should probably do something like this:

self = [super init];

if ([self isKindOfClass: [MyClass class]])
{
/* access MyClass instance vars here */
}

return self;

Ouch. I certainly wouldn't go that far, and definitely not in the general case.

Anyway, the pattern I use is the following:

-init
{
self=[super init];
[self setDict:[NSMutableDictionary dictionary]];
[self setArray:[NSMutableArray array];
return self;
}

So I use my accessors to avoid direct instance variable access. This protects me from self being nil without the test, and it even protects me from a different type of object being returned. (If the object being returned isn't compatible in the message-interface, then it is just broken).

Messaging is good.

Marcel

--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
f0249ab8b1af193ef5addcf39fdff5ca
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: semantics of init
      • From: j o a r <email@hidden>
    • Re: semantics of init
      • From: Christian Brunschen <email@hidden>
References: 
 >Re: semantics of init (From: email@hidden (Andrew Demkin))

  • Prev by Date: Instantiating NSCells in Interface Builder
  • Next by Date: Re: semantics of init
  • Previous by thread: Re: semantics of init
  • Next by thread: Re: semantics of init
  • Index(es):
    • Date
    • Thread