KVO and +initialize

Got caught by a really hard-to-diagnose issue today, so I decided to write it down in part so that you don’t get bitten by it, and partly so that next time I come across the issue, I’ll remember what it was.

I had a nasty bug in trying to add support for the AppleScript duplicate command to one of my objects. Now duplicate should, in principle, be simple: just conform to NSCopying and implement -copyWithZone:. The default implementation of NSCloneCommand should deal with everything else. But what I found was that there’s a class variable (OK, there isn’t, there’s a static in the class implementation file with accessors) with which the instances must compare some properties. And this was empty by the time the AppleScript ran. Well, that’s odd, thought I, it’s only being emptied once, and that’s when it’s created in +[MyClass initialize]. So what’s going on?

Having set a watchpoint on the static, I now know the answer: the +initialize method was being called twice. Erm, OK…why? It’s only called whenever a class is first used. It turns out that there were two classes with the same IMP for that method. The first was MyClass, and the second? NSKVONotifying_MyClass. Ah, great, Apple are adding a subclass of one of my classes for me!

It turns out that TFM has a solution:

+ (void)initialize
if (self == [MyClass class])
//real code

and I could use that solution here to fix my problem. But finding out that is the problem was a complete pig.

This entry was posted in applescript, cocoa, objc. Bookmark the permalink.

One Response to KVO and +initialize

  1. Unknown says:

    Interesting, thanks for that. I guess the moral is more general: in ObjC always assume you can be subclassed, even if you're client code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.