NSDictionary变成了NSCFString

时间:2013-02-11 22:16:00

标签: ios objective-c nsuserdefaults

我遇到一个奇怪的问题,即将NSDictionary存储到NSUserDefaults然后检索它会将其转换为NSCFString

这是我保存数据的地方:

- (void)saveProgress
{
    // Save our progress to the user defaults.
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *progressDict = [NSMutableDictionary dictionary];
    if ([defaults objectForKey:@"session_progress"] != nil) {
        [progressDict addEntriesFromDictionary:[defaults dictionaryForKey:@"session_progress"]];
    }
    NSMutableDictionary *targetsDict = [NSMutableDictionary dictionary];
    if ([progressDict objectForKey:@"targets"] != nil) {
        [targetsDict addEntriesFromDictionary:[progressDict objectForKey:@"targets"]];
    }
    NSMutableDictionary *trackableDict = [NSMutableDictionary dictionary];
    if ([targetsDict objectForKey:@"mda_sao_ext_04"]) {
        [trackableDict addEntriesFromDictionary:[targetsDict objectForKey:@"mda_sao_ext_04"]];
    }
    [trackableDict setObject:[NSNumber numberWithBool:YES]
                      forKey:@"viewed"];
    NSLog(@"SAVE Trackable %@ %@", [trackableDict class], trackableDict);
    [targetsDict setObject:trackableDict
                    forKey:@"mda_sao_ext_04"];
    NSLog(@"SAVE Targets %@ %@", [targetsDict class], targetsDict);
    [progressDict setObject:targetsDict
                     forKey:@"targets"];
    [defaults setObject:progressDict
                 forKey:@"session_progress"];
    [defaults synchronize];
}

这是我加载数据的地方:

- (NSInteger)loadProgress
{
    // Load our progress from the user defaults.
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"session_progress"] == nil) {
        return 0;
    }
    NSDictionary *progressDict = [defaults dictionaryForKey:@"session_progress"];
    if ([progressDict objectForKey:@"targets"] == nil) {
        return 0;
    }
    NSDictionary *targetsDict = [progressDict objectForKey:@"targets"];
    NSLog(@"LOAD Targets %@ %@", [targetsDict class], targetsDict);
    NSInteger viewedCount = 0;
    for (NSDictionary *trackableDict in targetsDict) {
        NSLog(@"LOAD Trackable %@ %@", [trackableDict class], trackableDict);
        if ([[trackableDict objectForKey:@"viewed"] boolValue]) {
            ++viewedCount;
        }
    }

    return viewedCount;
}

该应用程序在loadProgress崩溃。这是控制台的输出:

LOAD Targets __NSCFDictionary {
    "mda_sao_ext_04" =     {
        scanned = 1;
        viewed = 1;
    };
} 
LOAD Trackable __NSCFString mda_sao_ext_04

您可以从第一个跟踪中看到`mda_sao_ext_04是一个字典,但由于某种原因它被转换为字符串。

有任何想法或建议吗?谢谢!

2 个答案:

答案 0 :(得分:5)

NSDictionary的快速枚举返回键,而不是值。

for (NSDictionary *trackableDict in targetsDict) {

在这里你遍历所有键。变化

NSLog(@"LOAD Trackable %@ %@", [trackableDict class], trackableDict);

NSLog(@"LOAD Trackable %@ %@", [trackableDict class], targetsDict[trackableDict]);

答案 1 :(得分:4)

for (NSDictionary *trackableDict in targetsDict)

这个for构造遍历键,而不是值。所以,要获得价值,你必须

for (NSDictionary *trackableDict in [targetsDict allValues])

for (NSString *trackableKey in targetsDict)
{
    NSDictionary *trackableDict = targetsDict[trackableKey];
    .....
}