0

我有一个嵌套数组:

NSArray *cellInfos = @[
@[
  @{MenuCellId:@"1", MenuCellCounterKey:"0"},
  @{MenuCellId:@"2", MenuCellCounterKey:"0"}
],
@[
  @{MenuCellId:@"3", MenuCellCounterKey:"0"},
  @{MenuCellId:@"4", MenuCellCounterKey:"0"}
]];

如何通过 MenuCellId 更改 MenuCellCounterKey 值?

谢谢

4

1 回答 1

0

你需要复制数组深度,确保你得到的数组是可变的,并且nsdictionary也是可变的,然后遍历数组引用图层,比较MenuCellId,并更改MenuCellCounterKey。

NSMutableArray * mutableCellInfos = [cellInfos mutableCopy];
for (int i = 0; i < mutableCellInfos.count; ++i)
{
    NSMutableArray * mutableSection = [[mutableCellInfos objectAtIndex:i] mutableCopy];
    for (int j = 0; j < mutableSection.count; ++j)
    {
        NSMutableDictionary * mutableItem = [[mutableSection objectAtIndex:j] mutableCopy];
        if ([mutableItem objectForKey:@"3"]) {
            [mutableItem setObject:@"new value" forKey:@"3"];
        }

        [mutableSection replaceObjectAtIndex:j withObject:mutableItem];
    }

    [mutableSection replaceObjectAtIndex:i withObject:mutableSection];
}

cellInfos = mutableCellInfos;
于 2013-09-17T09:40:50.040 回答