Re: +initialize vs. +load
Re: +initialize vs. +load
- Subject: Re: +initialize vs. +load
- From: Bill Bumgarner <email@hidden>
- Date: Wed, 19 Mar 2003 11:46:22 -0500
On Wednesday, Mar 19, 2003, at 11:12 US/Eastern,
email@hidden wrote:
Hmm. My experimenting shows, and the docs claim that +load is called
before +initialize. But if you rewrite the paragraph above saying that
+initialize is the first useful general hook to override, we will be
friends again ;-)
+load is always invoked before +initialize *on any given class*.
However, NSObject's +initialize will often be invoked before some other
random class's +load or +initialize.
Bottom line: You cannot assume the order with which various +initalize
and +load methods will be called.
+load has some advantages over +initialize; namely, +load will be
invoked even if there are multiple implementations of said method on a
single class through categories. That is, if you implement two
categories of NSObject and both implement +load, both +load
implementations (and NSObject's, if it has one) will be executed!
This is not the case with +initialize.
+initialize will not be invoked unless something in the code tries to
use the class. +load will be invoked as the executable containing the
code is loaded/launched. That is, +load will be invoked when an
NSBundle is loaded and is often the only way to inject code into some
applications [when using Bundles, anyway -- there are other ways].
---
When using either method, I often leverage NSNotificationCenter or
-performSelector:withObject:afterDelay: to cause the real
initialization to be invoked as a normal part of app startup or to
force the code to be invoked during the first pass (or next pass)
through a run loop.
For both +load and +initialize, you typically need to create a local
NSAutoreleasePool to manage objects. Using either of the
aforementioned methods for pushing initialization into the run loop
also standardizes memory management in the context of the
initialization code.
b.bum
_______________________________________________
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.