Structure and Interpretation of Computer Programmers

I make it easier and faster for you to write high-quality software.

Wednesday, July 1, 2009

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.

posted by Graham Lee at 15:35  

Wednesday, September 24, 2008

AppleScript, for once

AppleScript isn’t something I write much about, in fact this is the first post I’ve ever created on the topic. But AppleScript, like the Services menu and Automator, provides that most useful of usability enhancements: the ability to use multiple applications together without fulfilling the marketing requirements of having to look at them all.

As an example, a folder action script might let me combine the Finder with any other application, such as, choosing completely at random, Sophos Anti-Virus:

on adding folder items to this_folder after receiving these_items

  set theScript to "/usr/bin/sweep -nc"

  repeat with i from 1 to number of items in these_items

    set thePath to POSIX path of item i of these_items

    set theScript to theScript & space & thePath

  end repeat

  set theScript to theScript & space & "--quarantine:mode=000"

  do shell script theScript

end adding folder items to

that script then scans any file which appears in a particular folder and locks it if it contains a virus (up to a point). But that’s not really the point, the point is that I haven’t actually had to use any of the target apps in order to get this combined functionality. It’s like I was able to summon the Megazord without having to actually talk to the individual Power Rangers. Erm, or something. And that, really, is how a computer should work; I didn’t buy OmniFocus so that I could look at its icon, or a splash screen, I bought it because it can manage my lists of things to do. And I got iCal in order to manage events in time. If I have things to do at specific times, then I ought to be able to combine the two, and the computer can do the work involved. After all, that is why I bought the computer.

posted by Graham Lee at 21:01  

Powered by WordPress