• 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: + initialize docs conflict?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: + initialize docs conflict?
      • From: Bill Cheeseman <email@hidden>
    • Re: + initialize docs conflict?
      • From: Simon Robins <email@hidden>
References: 
 >Re: + initialize docs conflict? (From: Greg Parker <email@hidden>)

  • Prev by Date: Re: + initialize docs conflict?
  • Next by Date: RE: Re: NSFileManager and rename file
  • Previous by thread: Re: + initialize docs conflict?
  • Next by thread: Re: + initialize docs conflict?
  • Index(es):
    • Date
    • Thread