views:

287

answers:

1

It seems from the bits and pieces I can scrape together that the answer to this one will be "no", so I'll broadly explain what I'm trying to achieve in (the likely) case that I'm trying to hammer in a screw.

What I have in my app is a list of valid file extensions to read in, which I'm recursing through a directory for. I want this to be a preference, so I've got the following:

NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:[NSMutableArray arrayWithObjects:@"pdf", @"rtf", @"txt", nil] forKey:@"validExtensions"];
[[NSUserDefaults standardUserDefaults] registerDefaults:dic];

and I'm binding the NSTableView to an NSArrayController which is bound to the Shared User Defaults controller...

Is this completely the wrong approach to having a user-customisable list of valid extensions? Have I misunderstood the role of NSUserDefaults?

+1  A: 

The answer to the question posed in the title is "Yes"... but unfortunately that doesn't help you, because the mutability of the data structures in the actual NSUserDefaults backing store isn't controlled by the mutability of the objects you pass in with registerDefaults:.

For preference values that are themselves structures, in order to change them you have to read the old structure, make a mutable copy of whatever portion you need to modify, alter that, and then set that structure as the new value. Unfortunately that's not well-suited to bindings, so you' d need a glue layer that mirrors changes in a bound array into the prefs.

smorgan
Ah, of course. I can't change the array itself, but I can replace the array in its entirety with -setObject:forKey:, thanks.
Drarok