【问题标题】:How to update NSMutableArray values inside the NSMutableDictionary如何更新 NSMutableDictionary 中的 NSMutableArray 值
【发布时间】:2014-06-10 06:30:13
【问题描述】:

我有一个 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)的第三个位置。

【问题讨论】:

    标签: ios objective-c xcode nsmutablearray nsmutabledictionary


    【解决方案1】:

    使用以下方式从您的字典中获取数组:

    NSMutableArray *arr = [yourDict ObjectForKey:@"key2"];
    

    通过以下方式替换数组中的对象:

    [arr replaceObjectAtIndex:indexPath.row withObject:newObject];
    

    如果你想把它放回去,

    [yourDict setObject:arr forKey:@"key2"];
    

    【讨论】:

    • 我知道我需要在可变字典中更新该数组
    • 它会自动反映在您的字典中。因为它们是引用类型。
    • 我还展示了将数组放回字典的方法。
    • 我再提一个问题你能帮我吗。stackoverflow.com/questions/24306138/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    相关资源
    最近更新 更多