Question

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.

Was it helpful?

Solution

Your dictionary must be like this:

NSDictionary *defaultSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
                                                         one, @"one", 
                                                         two, @"two", 
                                                         nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top