Re: What's up with this....
Re: What's up with this....
- Subject: Re: What's up with this....
- From: Chris Hanson <email@hidden>
- Date: Thu, 22 Mar 2012 12:39:08 -0700
On Mar 22, 2012, at 9:36 AM, Alexandra Beebe wrote: I have a class and in the class I added a "+(void) initialize" that just does a NSLog that it was in the class initialize function. Now when I include the class in a xib/nib file it works as expected. My NSLog message shows up. When I then hook a binding up that uses the class, everything breaks! My log shows 2 (two) NSLog initialize messages! Is this the correct list? Is this a bug? Is this the correct behavior? Am I missing something?
What you’re running into may be due to how bindings are implemented using Key-Value Observing under the hood: KVO can create a subclass of your class that wraps your accessors so they automatically send -willChangeValueForKey: and -didChangeValueForKey: at the right time. If you don’t guard the contents of your +initialize method, when this subclass receives +initialize your +initialize will be invoked. (Class methods are overridable in Objective-C, just like instance methods.)
Thus this is the pattern to use when implementing a +initialize method:
+ (void)initialize { if (self == [MyClass class]) { // MyClass-specific initialization } }
Of course, if you do want to do something for every subclass, you can put it outside the if block.
For example:
+ (void)initialize { NSLog(@"+initialize sent to %s", class_getName(self)); if (self == [MyClass class]) { NSLog(@"In +[MyClass initialize] specifically"); } }
Putting in that sort of logging may illustrate exactly why your +initialize is being invoked multiple times.
-- Chris
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden