【问题标题】:replace object at indexPath - big array with 12 sections替换 indexPath 处的对象 - 包含 12 个部分的大数组
【发布时间】:2016-01-28 11:29:18
【问题描述】:

我有一个包含 12 个部分的数组,我需要替换索引处的值。 我的测试代码:

NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:@{@"first": @[@"test1", @"test2"]}, @{@"second": @[@"test1"]}, nil];

NSLog(@"%@", [hm valueForKey:@"first"][0][0] );

[[hm valueForKey:@"first"][0] replaceObjectAtIndex:0 withObject:@"lol"];

NSLog(@"%@", hm);

第一个 NSLog 返回:test1 - 没关系

当替换- crash with -[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x7fde53d2f700

我需要将 test1 更改为一些东西。

请问我哪里做错了?

【问题讨论】:

  • 您的内部数组是不可变的 (NSArray),然后您需要是可变的 (NSMutableArray)。如果您避免使用嵌套数组并使用自定义对象,它会变得更加简单。
  • hm 是可变数组。当使用 [ hm replaceObjectAtIndex:0 withObject:@"lol"] - 它返回 lol 和 second
  • 你的 inner 数组是不可变的,例如@[@"test1", @"test2"].

标签: ios objective-c


【解决方案1】:
NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:@{@"first": @[@"test1", @"test2"]}, @{@"second": @[@"test1"]}, nil];

    NSLog(@"%@", [hm valueForKey:@"first"][0][0] );

    //Your inner array is immutable, change it to mutable and replace the object, That's it.
    NSMutableArray *array = [[hm valueForKey:@"first"][0] mutableCopy];
    [array replaceObjectAtIndex:0 withObject:@"lol"];
    [hm replaceObjectAtIndex:0 withObject:array];

    NSLog(@"%@", hm);  

【讨论】:

    【解决方案2】:
    NSMutableArray *arr = [[NSMutableArray alloc] initwithArray[[hm objectAtIndex:0]objectForKey:@"first"]];
    [arr replaceObjectAtIndex:0 withObject:@"lol"];
    [hm replaceObjectAtIndex:0 withObject:arr];
    

    【讨论】:

      【解决方案3】:

      你必须制作可变字典和数组结构

      NSMutableArray* names = [NSMutableArray arrayWithObjects:
                               [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                @"Joe",@"firstname",
                                @"Bloggs",@"surname",
                                nil],
                               [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                @"Simon",@"firstname",
                                @"Templar",@"surname",
                                nil],
                               [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                @"Amelia",@"firstname",
                                @"Pond",@"surname",
                                nil],
                               nil];
      
      NSLog(@"Before - %@",names);
      
      
      [names replaceObjectAtIndex:0 withObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                @"Joe_New",@"firstname",
                                                @"Bloggs_New",@"surname",
                                                nil]];
      
      NSLog(@"After - %@",names);
      

      之前 - ( { 名字=乔; 姓氏=博客; }, { 名字=西蒙; 姓氏=圣殿骑士; }, { 名字 = 阿米莉亚; 姓氏=池塘; } )

      之后 - ( { 名字 = "Joe_New"; surname = "Bloggs_New"; }, { 名字=西蒙; 姓氏=圣殿骑士; }, { 名字=阿米莉亚; 姓氏=池塘; } )

      【讨论】:

        【解决方案4】:
        NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:@{@"first": @[@"test1", @"test2"]}, @{@"second": @[@"test1"]}, nil];
        
            NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithDictionary:[hm objectAtIndex:0]];
            NSMutableArray *array=[NSMutableArray arrayWithArray:[dic objectForKey:@"first"]];
            [array replaceObjectAtIndex:0 withObject:@"lol"];
        
            [dic setObject:array forKey:@"first"];
        
            [hm replaceObjectAtIndex:0 withObject:array];
        
        
        
        
        
         O/P  (ViewController.m:48) (
                    (
                    lol,
                    test2
                ),
                    {
                    second =         (
                        test1
                    );
                }
            )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多