Re: Binding to an array of NSStrings stored in an application's preferences
Re: Binding to an array of NSStrings stored in an application's preferences
- Subject: Re: Binding to an array of NSStrings stored in an application's preferences
- From: Rob Keniger <email@hidden>
- Date: Thu, 30 Jul 2009 13:40:07 +1000
On 30/07/2009, at 8:15 AM, A B wrote:
I have an application where I'd like to store an editable set of
keywords by way of the preferences controller. Easy, right?
Unfortunately, for the life of me I can't figure out how to do it.
I set up an array controller and point it to the shared preferences
object and give it a keypath for the array. The problem is that the
objects being stored in the array are NSStrings and I don't see how
NSStrings can be KVC-compliant for this kind of operation due to the
lack of any "stringValue/setStringValue" methods. Without those,
what keypath do I use in the NSTableView I have set up to facilitate
manipulation of that list?
Also, since the shared preferences object is not one that I own, I'm
not sure how to go about setting up a new array to represent an
unedited/newly installed state. (Registering them as defaults
doesn't seem to be working for me.)
You can't do this with an array of NSStrings, you have to work with
strings encapsulated as properties of an object, such as an
NSDictionary. So you need an array of dictionaries which contain a
string object for a particular key.
Something like:
NSArray* prefs = [NSArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObject:@"One" forKey:@"name"],
[NSMutableDictionary dictionaryWithObject:@"Two" forKey:@"name"],
[NSMutableDictionary dictionaryWithObject:@"Three" forKey:@"name"],
nil];
NSDictionary* defaults = [NSDictionary dictionaryWithObject:prefs
forKey:@"MyDefaults"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
Then you can do this:
- instantiate an NSArrayController in your nib and set its Class Name
field to NSMutableDictionary (the default).
- bind it to the Shared User Defaults Controller with a Controller Key
of "values" and a Model Key that is the preferences key you want to
store/edit (MyDefaults in the example above).
- make sure you check the "Handles content as a compound value"
checkbox or editing will not work.
- bind the value of the table column to your NSArrayController's
arrangedObjects key with a Model Key of "name" or whatever you set up
in your dictionary.
--
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