NSUserDefaults won't do as it's told
NSUserDefaults won't do as it's told
- Subject: NSUserDefaults won't do as it's told
- From: email@hidden
- Date: Sat, 3 Mar 2007 16:28:01 +0100 (CET)
- Importance: Normal
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:
This email sent to email@hidden