2014-04-20 55 views
0

我認爲我在這裏做了一些非常明顯的錯誤,但我弄不明白!我試圖將包含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"]; 

我已經試過印刷字典中的所有子對象的類別,而且他們所有的標準,我也正在以同樣的方式構建另一個字典,並且工作正常。

請有人指出我的錯誤?

謝謝!

回答

9

在plist中,鍵必須是字符串。由於NSUserDefaults由plist支持,因此它會強制執行此限制。

作爲一個旁註,我認爲你會更清楚地使用[@{...} mutableCopy]

+0

啊太棒了!謝謝!還有很好的技巧mutableCopy謝謝:) – user2851943

+0

我面臨同樣的問題。你能澄清,如果有可能將字典保存爲NSUserDefaults的字符串對象? –

+0

@techsavvy是的,你可以有一個字典作爲一個值,但該字典中的每個鍵也必須是一個字符串。 – Kevin

1

NSUserDefault僅支持NSStringNSNumberNSDateNSArrayNSDictionaryBOOLNSIntegerNSFloat等基本數據類型。

如果這些集合類型包含其他非系統數據類型,它們仍可能導致崩潰。如果您包含自定義數據類型,則需要將其轉換爲NSData存儲。 This is my test demo

相關問題