문제

I think I'm doing something really obviously wrong here, but I can't figure it out! I'm attempting to save an NSMutableDictionary containing NSStrings and NSNumbers and NSBooleans into NSUserDefaults on iOS 7.1.

Here's the dictionary definition:

- (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;
}

And saving it:

[defaults setObject:self.hotLevelsDict forKey:@"hotLevelsDict"];

I've tried printing the classes of all the sub objects within the dictionary, and they're all standard, also I'm doing the same thing to another dictionary constructed in a very similar way and it works fine.

Please could someone point out my error?

Thanks!

도움이 되었습니까?

해결책

In a plist, the keys have to be strings. Since NSUserDefaults is backed by a plist, it enforces that restriction.

As a sidenote, I think it would be clearer for you to use [@{...} mutableCopy].

다른 팁

The NSUserDefault only supports NSString, NSNumber, NSDate, NSArray, NSDictionary, BOOL, NSInteger, NSFloat and other basic data types.

These collection types can still cause crashes if they contain other non - system data types. If you include a custom data type, you need to convert to NSData storage. This is my test demo

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top