Thursday, 12 September 2013

only property-list objects (NSNumber) in NSDictionary but not able to store dict in UserDefaults

only property-list objects (NSNumber) in NSDictionary but not able to
store dict in UserDefaults

I have an NSDictionary with NSNumbers as Values and Keys. These numbers
represent certain settings in the app and I would like to store the
dictionary in UserDefaults. Since NSNumbers are a property-list object, i
thought this should work, but i retrieve the following error:
*** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property
value '{ 2 = 1; }' of class '__NSDictionaryM'. Note that dictionaries and
arrays in property lists must also contain only property values.
The (simplified) test-code I use is:
NSNumber *one = [NSNumber numberWithInt:1];
NSNumber *two = [NSNumber numberWithInt:2];
NSDictionary *defaultSetting = [[NSDictionary alloc]
initWithObjectsAndKeys: one, two, nil];
[[NSUserDefaults standardUserDefaults] setObject:defaultSetting
forKey:@"settings"];
Why doesn't this work? I do not see what's wrong with it?
Thanks for your time and replies.

No comments:

Post a Comment