Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone IOS中嵌套的NSArray值更改_Iphone_Ios_Nsarray - Fatal编程技术网

Iphone IOS中嵌套的NSArray值更改

Iphone IOS中嵌套的NSArray值更改,iphone,ios,nsarray,Iphone,Ios,Nsarray,我有一个嵌套数组: NSArray *cellInfos = @[ @[ @{MenuCellId:@"1", MenuCellCounterKey:"0"}, @{MenuCellId:@"2", MenuCellCounterKey:"0"} ], @[ @{MenuCellId:@"3", MenuCellCounterKey:"0"}, @{MenuCellId:@"4", MenuCellCounterKey:"0"} ]]; 如何通过MenuCellId更改Menu

我有一个嵌套数组:

NSArray *cellInfos = @[
@[
  @{MenuCellId:@"1", MenuCellCounterKey:"0"},
  @{MenuCellId:@"2", MenuCellCounterKey:"0"}
],
@[
  @{MenuCellId:@"3", MenuCellCounterKey:"0"},
  @{MenuCellId:@"4", MenuCellCounterKey:"0"}
]];
如何通过MenuCellId更改MenuCellCounterKey值


谢谢

您需要复制数组深度,确保您获得的数组是可变的,并且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;
NSMutableArray*mutableCellInfos=[cellInfos mutableCopy];
对于(int i=0;i
首先,您需要
NSMutableArray
,因为
NSArray
无法修改。请更简要地解释您的问题。为什么有两个相同类型的字典数组?这是表视图中的两个或更多部分。在我的例子中,MenuCellCounterKey和MenuCellId是一个常量:NSString const*MenuCellId=@“CellId”