Objective-C访问/更改多维数组中的数组元素(NSArray)

dan*_*dan 5 arrays objective-c multidimensional-array

我正在尝试更改多维数组中的值,但收到编译器错误:

warning: passing argument 2 of 'setValue:forKey:' makes pointer from integer without a cast
Run Code Online (Sandbox Code Playgroud)

这是我的内容数组:

NSArray *tableContent = [[NSArray alloc] initWithObjects:
                [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil],
                [[NSArray alloc] initWithObjects:@"d",@"e",@"f",nil],
                [[NSArray alloc] initWithObjects:@"g",@"h",@"i",nil],
                 nil];
Run Code Online (Sandbox Code Playgroud)

这就是我试图改变价值的方式:

[[tableContent objectAtIndex:0] setValue:@"new value" forKey:1];
Run Code Online (Sandbox Code Playgroud)

解:

 [[tableContent objectAtIndex:0] setValue:@"new val" forKey:@"1"];
Run Code Online (Sandbox Code Playgroud)

所以数组键是一个字符串类型 - 有点奇怪,但很高兴知道.

dre*_*lax 11

NSMutableArray *tableContent = [[NSMutableArray alloc] initWithObjects:
                    [NSMutableArray arrayWithObjects:@"a",@"b",@"c",nil],
                    [NSMutableArray arrayWithObjects:@"d",@"e",@"f",nil],
                    [NSMutableArray arrayWithObjects:@"g",@"h",@"i",nil],
                     nil];

[[tableContent objectAtIndex:0] replaceObjectAtIndex:1 withObject:@"new object"];
Run Code Online (Sandbox Code Playgroud)

您不希望alloc+init子阵列因为子阵列的保留计数太高(+1 alloc,因为它插入外部阵列时再次+1).