Re: Cocoa Bindings and NSUserDefaults
Re: Cocoa Bindings and NSUserDefaults
- Subject: Re: Cocoa Bindings and NSUserDefaults
- From: Rob Keniger <email@hidden>
- Date: Wed, 25 Nov 2009 12:51:15 +1000
On 25/11/2009, at 12:17 PM, Ben Haller wrote:
> - There's a little bit of duplication of information between the nib and the code. The tags on the radio button matrix's cells have to match the values in the enumeration, for one thing. Even more annoyingly, the strings for the defaults keys have to correspond to the bindings key paths I enter in IB. This duplication seems like a vulnerability (not that it wouldn't also be there without using bindings, of course). There isn't any reasonable way to get rid of this, is there?
Unfortunately not. Key paths are just strings and you can't use symbolic constants in Interface Builder, you have to use the string itself. Yes, it's fragile.
> - A little bit of glue code still seems to be necessary (or at least aesthetic) to vend the user defaults to client code (i.e. the +launchAction, +sgeHeadNode, etc. class methods). I could get rid of them, but then I'd have to export the defaults keys in the header, and every client of the preferences would have to do the same thing as this glue code themselves, so the current way seems preferable (no pun intended :->). Am I missing a way to make this code nicer, or will I have to add a glue method like this every time I add a defaults key?
When you retrieve and initialize the values you can just use regular user defaults, you don't have to use the NSUserDefaultsController. The user defaults controller that you use in bindings just pipes the values directly into NSUserDefaults so unless you need the additional functionality of the user defaults controller in your code you can ignore it.
+ (void)initialize
{
NSDictionary *defaults = [NSDictionary dictionaryWithObjectsAndKeys:
@"my.server.com", akSGEHeadNodeKey,
@"username", akSGEUsernameKey,
@"password", akSGEPasswordKey,
[NSNumber numberWithInt:akLaunchShowNewSimulationPanel], akLaunchActionKey,
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
+ (AKLaunchAction)launchAction
{
return (AKLaunchAction)[[[NSUserDefaults standardUserDefaults] objectForKey:akLaunchActionKey] intValue];
}
--
Rob Keniger
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden