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”