Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSUserDefaults won't do as it's told



 Hello all,

 I'm trying to add preference features to my Cocoa app using NSUserDefaults.
To do that I mimic the example in Chapter 9 of Aaron Hillegass' book : the
initial default values are created and registered by [NSUserDefaults
standardUserDefaults]
in the "initialize" class method of the main app's delegate. What I don't
understand
is that, when the program has just finished executing this "initialize"
method and
I check in the debugger if [NSUserDefaults standardUserDefaults] knows
about the
data it was just asked to remember :

(gdb) po [NSUserDefaults standardUserDefaults]
<NSUserDefaults: 0x30a8f0>
(gdb) po GGPeggyHeaderContentKey
HeaderContents
(gdb) po [[NSUserDefaults standardUserDefaults]
objectForKey:GGPeggyHeaderContentKey]
Cannot access memory at address 0x0


   That is, the return value is nil, and the code just executed was all in
vain ! As usual, I
must be missing something obvious ...
   Below is the aforementioned code for the "initialize" method :

+(void) initialize
{
   NSMutableDictionary* defaultValues=[NSMutableDictionary dictionary];
   NSData* yellowData=[NSKeyedArchiver archivedDataWithRootObject:[NSColor
yellowColor]];
   NSData* greenData=[NSKeyedArchiver archivedDataWithRootObject:[NSColor
greenColor]];
   [defaultValues setObject:[[NSAttributedString alloc]
initWithString:@"\n%"] forKey:GGPeggyHeaderContentKey];
   [defaultValues setObject:[[NSAttributedString alloc]
initWithString:@"\n%"] forKey:GGPeggyFooterContentKey];
   [defaultValues setObject:yellowData forKey:GGPeggyHeaderColorKey];
   [defaultValues setObject:greenData forKey:GGPeggyFooterColorKey];
   [defaultValues setObject:[NSNumber numberWithBool:YES]
forKey:GGMultiplePageViewBooleanKey];

   [[NSUserDefaults standardUserDefaults] registerDefaults: defaultValues];

}


  Note : there are no warnings during the compile phase.

                                                                                                           Ewan



_______________________________________________

Cocoa-dev mailing list (email@hidden)

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/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.