Re: + initialize docs conflict?
Re: + initialize docs conflict?
- Subject: Re: + initialize docs conflict?
- From: Ricky Sharp <email@hidden>
- Date: Tue, 01 Feb 2005 13:50:27 -0600
On Tuesday, February 01, 2005, at 01:39PM, Greg Parker <email@hidden> wrote:
>> When reading up on NSObject's class method 'initialize', I found these
>> two quotes in the docs:
>>
>> "Each class receives the initialize message just once from the runtime
>> system."
>>
>> "Normally the runtime system sends a class just one initialize
>> message. However, if for some reason an application or the runtime
>> system generates additional initialize messages, it is a good idea to
>> prevent code from being invoked more than once"
>>
>> I can see where an application developer may (accidentally?) have code
>> to call initialize more than once. But why do the docs have
>> conflicting statements regarding the runtime?
>
>I see that you've filed a bug against the documentation, which is good.
>To clarify, here's what really happens in the runtime:
>
>* The runtime sends +initialize to each class exactly zero or one
>times. A superclass always receives its +initialize before any of its
>subclasses. This is all done thread-safely in 10.2 and later.
>* If a particular class does not implement +initialize, its
>superclass's +initialize will be called twice.
>* Some classes calls [super initialize] inside their own +initialize.
>This is discouraged.
None of my code calls [super initialize], so good to know I'm not doing discouraged.
>The typical "one-time only" implementation looks like this:
>
>@implementation MyClass
>+initialize {
> if (self == [MyClass class]) {
> // do something once
> }
>}
mmalc also pointed this out in another e-mail. I've been using this pattern already for +initialize in my NSControl subclasses. I plan to use this pattern for all my initialize code (some still use a static flag).
>In both "multiple +initialize" cases listed above, `self` is some
>subclass of MyClass in the duplicate calls, so the real code only runs
>once. Of course, if someone were to call `[MyClass initialize]`
>explicitly the check wouldn't work, but that shouldn't actually happen.
Thanks for this detailed description of what really happens. It all makes much more sense now.
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden