• 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: What's up with this....
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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?

The best list to use for Cocoa help is email@hidden, or you can use the Cocoa group in the Developer Forums <https://devforums.apple.com/community/mac/appframeworks/cocoa>.

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

References: 
 >Re: What's up with this.... (From: Alexandra Beebe <email@hidden>)

  • Prev by Date: Re: Xcode-users Digest, Vol 9, Issue 157
  • Next by Date: Re: where is this file
  • Previous by thread: Re: What's up with this....
  • Next by thread: major 4.3.2 debugging regression
  • Index(es):
    • Date
    • Thread