我有一个NSDictionary,NSNumbers是值和键。这些数字代表应用程序中的某些设置,我想将字典存储在UserDefaults中。由于NSNumbers是属性列表对象,我认为这应该工作,但我检索以下错误:
*** -[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.
我使用的(简化)测试代码是:
NSNumber *one = [NSNumber numberWithInt:1];
NSNumber *two = [NSNumber numberWithInt:2];
NSDictionary *defaultSetting = [[NSDictionary alloc] initWithObjectsAndKeys: one, two, nil];
[[NSUserDefaults standardUserDefaults] setObject:defaultSetting forKey:@"settings"];
为什么这不起作用?我不明白它有什么问题?
感谢您的时间和回复。
答案 0 :(得分:6)
你的词典必须是这样的:
NSDictionary *defaultSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
one, @"one",
two, @"two",
nil];