a glob of nerdishness

September 15, 2007

[widget setPreference: for:] is not JavaScript!

written by natevw @ 12:08 pm

Widgets can get and set persistent preferences without much hassle. It’s as easy as calling widget.preferenceForKey(key), which will return the value set for the key.

However, sometimes this doesn’t seem to work, because the developer swapped the arguments when setting. The correct order widget.setPreferenceForKey(value, key) is swapped from, say, element.setAttribute(name,value). This one has gotten me way too many times when developing Dashboard widgets.

How did this bizarre ordering come about? I’m guessing it’s because the widget object is actually written as an Objective-C plugin and the developers felt obligated to make the method idiomatic in that language: (void)setPreference:(NSString*)value forKey:(NSString*)key. But the function name is rewritten between Obj-C and JS anyway — I wish they could have broken the idiom a bit and used (void)setPreference:(NSString*)key toValue:(NSString*)value as the method signature, for the sake of the end users.

(Updated to fix method names from original post.)