Dear list,

The hardest part of this problem may be explaining it, but here goes.

I want to store an array of dictionaries in NSUserDefaults. In each dictionary 
I have a string and a color. The color is stored as data using NSArchiver.

So I build an array of these dictionaries, then put that array in another 
dictionary, which becomes the registered defaults.

So in my app delegate +initialize: I have something like:

        NSMutableDictionary *item1 = [[[NSMutableDictionary alloc] 
initWithCapacity:1] autorelease];
        NSMutableDictionary *item2 = [[[NSMutableDictionary alloc] 
initWithCapacity:1] autorelease];
        [item1 setValue:@"name1" forKey:@"name" ];
        [item1 setValue:[NSArchiver archivedDataWithRootObject:[NSColor 
blueColor]] forKey:@"color"];
        [item2 setValue:@"name1" forKey:@"name" ];
        [item2 setValue:[NSArchiver archivedDataWithRootObject:[NSColor 
redColor]] forKey:@"color"];
        
        NSArray *catArray = [NSArray arrayWithObjects:item1,item2, nil];
        
        // add defaults to dictionary
        NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary]; 
        [defaultValues setObject:catArray forKey:@"MyItemList"];
        
        // register the defaults
        [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];

Now comes the UI. I have a nib with a preferences panel. On the panel I have a 
table with two columns: one for 'name' and one for 'color'. So I put an array 
controller in the nib and bind it to userdefaults.values.MyItemList with the 
'Handles Content As Compound Value' field checked (I read somewhere this was 
necessary). Then I bind the columns of the table to the array controller. For 
the 'color' column I have a value transformer NSUnarchiveFromData. The 'color' 
column uses a custom cell which fires a callback in the preferences controller 
to launch a color panel. The action of the color panel is set to the following 
selector:

- (void) colorChanged: (id) sender 
{
        NSArray *items = [itemArrayController arrangedObjects];
        NSDictionary *item = [items objectAtIndex:[defaultItemsTable 
selectedRow] ];
        [item setValue:[NSArchiver archivedDataWithRootObject:[sender color]] 
forKey:@"color"];
        [defaultItemsTable setNeedsDisplay:YES];
} 

Now the problem. I can change the 'name' values and they stay changed between 
app launches. I can change colors and they change in the table, no problem. 
However, the new colors are not written to the user defaults. So if I relaunch 
the app the colors are back to the factory defaults. If I try to access the 
user defaults during the same running instance of the app, they are still the 
factory defaults. I can also add new items to the array controller, and they 
appear as they should on app relaunch. It's just the colors that are causing 
trouble.

Has anyone tried something like this, or can anyone see what I'm doing wrong?

Thanks in advance,

Martin


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Martin Hewitson
Albert-Einstein-Institut
Max-Planck-Institut fuer 
    Gravitationsphysik und Universitaet Hannover
Callinstr. 38, 30167 Hannover, Germany
Tel: +49-511-762-17121, Fax: +49-511-762-5861
E-Mail: martin.hewit...@aei.mpg.de
WWW: http://www.aei.mpg.de/~hewitson
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to