NSUserDefaults setObject:forKey:尝试插入非属性列表对象

时间:2014-04-20 18:19:46

标签: ios objective-c dictionary nsuserdefaults

我觉得我在这里做的事情显然是错的,但我无法理解!我正在尝试将包含NSStrings和NSNumbers以及NSBooleans的NSMutableDictionary保存到iOS 7.1上的NSUserDefaults中。

这是字典定义:

- (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"];

我已经尝试打印字典中所有子对象的类,它们都是标准的,我也在用另一种以非常相似的方式构建的字典做同样的事情,它工作正常。

请有人指出我的错误吗?

谢谢!

2 个答案:

答案 0 :(得分:9)

在plist中,键必须是字符串。由于NSUserDefaults由plist支持,因此它强制执行该限制。

作为旁注,我认为使用[@{...} mutableCopy]会更清楚。

答案 1 :(得分:2)

NSUserDefault仅支持NSStringNSNumberNSDateNSArrayNSDictionaryBOOL,{{1 },NSInteger和其他基本数据类型。

如果这些集合类型包含其他非系统数据类型,则仍会导致崩溃。如果您包含自定义数据类型,则需要转换为NSFloat存储空间。 This is my test demo