我認爲我在這裏做了一些非常明顯的錯誤,但我弄不明白!我試圖將包含NSString和NSNumbers和NSBooleans的NSMutableDictionary保存到iOS 7.1的NSUserDefaults中。NSUserDefaults setObject:forKey:嘗試插入非屬性列表對象
這裏的字典中的定義:
- (NSMutableDictionary *)hotLevelsDict
{
if ((!_hotLevelsDict) || ([_hotLevelsDict count] < 1))
{
_hotLevelsDict = [NSMutableDictionary dictionaryWithDictionary:
@{@100: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @50, @"complete": @NO}],
@200: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @100, @"complete": @NO}],
@500: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @200, @"complete": @NO}],
@1000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @200, @"complete": @NO}],
@2000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @500, @"complete": @NO}],
@5000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@10000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@20000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @10, @"points": @1000, @"complete": @NO}],
@50000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@100000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@200000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@300000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@400000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@500000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@600000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@700000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@800000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @20, @"points": @1000, @"complete": @NO}],
@900000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @5, @"points": @50000, @"complete": @NO}],
@950000: [NSMutableDictionary dictionaryWithDictionary:@{@"time": @2, @"points": @50000, @"complete": @NO}]
}];
}
return _hotLevelsDict;
}
,並將其保存:
[defaults setObject:self.hotLevelsDict forKey:@"hotLevelsDict"];
我已經試過印刷字典中的所有子對象的類別,而且他們所有的標準,我也正在以同樣的方式構建另一個字典,並且工作正常。
請有人指出我的錯誤?
謝謝!
啊太棒了!謝謝!還有很好的技巧mutableCopy謝謝:) – user2851943
我面臨同樣的問題。你能澄清,如果有可能將字典保存爲NSUserDefaults的字符串對象? –
@techsavvy是的,你可以有一個字典作爲一個值,但該字典中的每個鍵也必須是一個字符串。 – Kevin