在for循环中设置变量

时间:2014-01-21 01:25:42

标签: ios objective-c for-loop

我编写了一些代码来过滤字典并将URL更改为其UIImage表示。这是我的代码:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSDictionary *json = appDelegate.json;

for (id section in json) {
    NSArray *key = [json objectForKey:section];
    if ([key isKindOfClass:[NSArray class]]) {
        for (NSMutableDictionary __strong *entry in key) {
            entry = [entry mutableCopy];
            for (id value in [entry allKeys]) {
                if ([value isEqualToString:@"pic"]) {
                    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[entry objectForKey:value]]];
                    UIImage *image = [UIImage imageWithData:data];
                    [entry setObject:image forKey:value];
                }
            }
        }
    }
}

appDelegate.json = json;

迭代是正确的,因为当我断开我的代码时,我看到了一个用于pic而不是字符串的UIImage变量。但是,我发现如果我继续执行,它会在我退出for循环时恢复。

我的数据结构非常奇怪,所以我提供了它的截图here

4 个答案:

答案 0 :(得分:1)

您的代码在此entry = [entry mutableCopy];,您创建了一个新的NSMutableDictionary并将其分配给条目,您所做的一切只会对它产生影响。

答案 1 :(得分:0)

请添加Json,以便我们更容易纠正答案 但是从你的代码中清楚地知道你正在改变你正在循环的字典。哪个不行!您需要设置不同的Mutable Dictionary并分配这些值。像这样的东西

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSDictionary *json = appDelegate.json;
    NSMutableDictionary *finalJson =  [[NSMutableDictionary alloc] init];
    for (id section in json) {
        NSArray *key = json[section];
        if ([key isKindOfClass:[NSArray class]]) {
            for (NSDictionary *entry in key) {
                for (id value in [entry allKeys]) {
                    if ([value isEqualToString:@"pic"]) {
                        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:entry[value]]];
                        UIImage *image = [UIImage imageWithData:data];
                        [finalJson setObject:image forKey:value];
                    }
                }
            }
        }
    }

    appDelegate.json = finalJson;

这将创建只有图像的finalJson字典..如果这是你需要的。 但是如果你需要带有其他值的finalJson,那么你需要在每个循环中分配对象

答案 2 :(得分:0)

@rmaddy完全正确。我试图更改entry,并期望它也更新json。我最后改变了这个:

[entry setObject:image forKey:value];

到此:

// Changes the json variable directly
[[[json objectForKey:section] objectAtIndex:i - 1] setObject:image forKey:value];

答案 3 :(得分:0)

应该是:

NSMutableDictionary *json = [appDelegate.json mutableCopy];

完全是:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary *json = [appDelegate.json mutableCopy];

for (id section in json) {
    NSArray *key = [json objectForKey:section];
    if ([key isKindOfClass:[NSArray class]]) {
        for (NSMutableDictionary __strong *entry in key) {
            entry = [entry mutableCopy];
            for (id value in [entry allKeys]) {
                if ([value isEqualToString:@"pic"]) {
                    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[entry objectForKey:value]]];
                    UIImage *image = [UIImage imageWithData:data];
                    [entry setObject:image forKey:value];
                }
            }
        }
    }
}