Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 839
HesselM Avatar asked Oct 23 '25 15:10

HesselM


1 Answers

Your dictionary must be like this:

NSDictionary *defaultSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
                                                         one, @"one", 
                                                         two, @"two", 
                                                         nil];
like image 130
lykant Avatar answered Oct 26 '25 11:10

lykant