我有一个 NSMutableDictionary Test = {key1:[0,0,0,0],key2:[0,0,8,0,0]}..这里的数组是 NSMutableArray。我需要更新一些值字典内的数组。例如。
- (IBAction)update(id)sender {
UIButton *button = (UIButton *)sender;
int test = 9;
UIView *contentView = button.superview;
UIView *viewWithTag16 = [contentView viewWithTag:16];
NSIndexPath *indexPath = [self.dataColletionview indexPathForCell: (DataColletionViewCell *)[[sender superview]superview]];
NSLog(@"flag values...%@",_flag);
......................
.....................
}
这里 indexpath.row 将是一个可变整数,标志将像 key1,key2,key3 类似于字典中的键。基于整数 a 值和 _flag 值,我需要更新字典中的数组值。示例_flag = key2 和 indaexpath.row = 3。我需要将测试整数值更新到第二个数组(key2)的第 3 个位置。
最佳答案
使用以下方式从字典中获取数组:
NSMutableArray *arr = [yourDict ObjectForKey:@"key2"];
通过以下方式替换数组中的对象:
[arr replaceObjectAtIndex:indexPath.row withObject:newObject];
如果你想把它放回去,
[yourDict setObject:arr forKey:@"key2"];
关于ios - 如何更新 NSMutableDictionary 中的 NSMutableArray 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24134321/