1

我遇到了一个奇怪的问题,将 an 存储NSDictionary到 theNSUserDefaults然后检索它会将其转换为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 是一个字典,但由于某种原因它被转换为一个字符串。

有什么想法或建议吗?谢谢!

4

2 回答 2

5

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

for (NSDictionary *trackableDict in targetsDict) {

在这里,您遍历所有键。改变

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

NSLog(@"LOAD Trackable %@ %@", [trackableDict class], targetsDict[trackableDict]);
于 2013-02-11T22:23:43.640 回答
4
for (NSDictionary *trackableDict in targetsDict)

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

for (NSDictionary *trackableDict in [targetsDict allValues])

或者

for (NSString *trackableKey in targetsDict)
{
    NSDictionary *trackableDict = targetsDict[trackableKey];
    .....
}
于 2013-02-11T22:21:54.310 回答