• 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: Greg Parker <email@hidden>
  • Date: Tue, 1 Feb 2005 11:39:02 -0800

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.


The typical "one-time only" implementation looks like this:

@implementation MyClass
+initialize {
    if (self == [MyClass class]) {
        // do something once
    }
}

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.


-- Greg Parker email@hidden Runtime Wrangler


_______________________________________________ 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: Ricky Sharp <email@hidden>
  • Prev by Date: Re: NSFileManager and rename file
  • Next by Date: catching mouseDown in NSTableView
  • Previous by thread: Re: + initialize docs conflict?
  • Next by thread: Re: + initialize docs conflict?
  • Index(es):
    • Date
    • Thread