Ios 将indexPath处的对象-大数组替换为12个部分

Ios 将indexPath处的对象-大数组替换为12个部分,ios,objective-c,Ios,Objective C,我有一个包含12个部分的数组,需要替换索引处的值。 我的测试代码: NSMutableArray *hm = [[NSMutableArray alloc] initWithObjects:@{@"first": @[@"test1", @"test2"]}, @{@"second": @[@"test1"]}, nil]; NSLog(@"%@", [hm valueForKey:@"first"][0][0] ); [[hm valueForKey:@"first"][0] replac

我有一个包含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
-正常

当replace
-crash为-[\uuu NSArrayI replaceObjectAtIndex:withObject:]:无法识别的选择器发送到实例0x7fde53d2f700

我需要把test1改成别的东西

请问我做错了什么

NSMutableArray *arr = [[NSMutableArray alloc] initwithArray[[hm objectAtIndex:0]objectForKey:@"first"]];
[arr replaceObjectAtIndex:0 withObject:@"lol"];
[hm replaceObjectAtIndex:0 withObject:arr];

您必须创建可变字典和数组结构

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);
在—( { 名字=乔; 姓氏=布洛格斯; }, { 名字=西蒙; 姓氏=圣堂武士; }, { 名字=阿米莉亚; 姓氏=池塘; } )

在-( { firstname=“Joe_New”; 姓氏=“Bloggs_New”; }, { 名字=西蒙; 姓氏=圣堂武士; }, { 名字=阿米莉亚; 姓氏=池塘; }
)

您的内部数组是不可变的(
NSArray
),然后您需要是可变的(
NSMutableArray
)。如果您避免使用嵌套数组,而是使用自定义对象,它将变得更加简单。hm是可变数组。当使用[hm replaceObjectAtIndex:0 with Object:@“lol”]-它返回lol,然后您的内部数组是不可变的,例如
@[@“test1”、@“test2”]
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);
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
            );
        }
    )