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

Objective-C访问/更改多维数组(NSArray)中的数组元素,objective-c,arrays,multidimensional-array,Objective C,Arrays,Multidimensional Array,我试图更改多维数组中的值,但遇到编译器错误: warning: passing argument 2 of 'setValue:forKey:' makes pointer from integer without a cast 这是我的内容数组: NSArray *tableContent = [[NSArray alloc] initWithObjects: [[NSArray alloc] initWithObjects:@"a",@"b",@"c",ni

我试图更改多维数组中的值,但遇到编译器错误:

warning: passing argument 2 of 'setValue:forKey:' makes pointer from integer without a cast
这是我的内容数组:

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];
这就是我试图更改值的方式:

[[tableContent objectAtIndex:0] setValue:@"new value" forKey:1];
解决方案:

 [[tableContent objectAtIndex:0] setValue:@"new val" forKey:@"1"];

所以数组键是一种字符串类型-有点奇怪,但知道它很好。

您正在创建不可变数组,并试图更改其中存储的值。使用NSMutableArray。

您需要NSMutableArray的
插入对象:atIndex:
替换对象atIndex:withObject:
(如果现有元素已经存在,前者将推回,而后者将替换它,但对于尚未占用的索引不起作用)。消息
setValue:forKey:
的第一个参数采用值类型,第二个参数采用NSString。您传递的是一个整数,而不是NSString,因为NSString永远无效

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"];

您不希望对子数组执行
alloc+init
,因为子数组的保留计数将太高(+1)对于
alloc
,然后在插入外部数组时再次执行+1)。

很抱歉回答1年半前的问题:D

我遇到了同样的问题,最后我通过计算元素数解决了这个问题,然后执行
addObject
推送到数组元素

没有类似setObject:atIndex:但setObject:forKey:的消息。但是仍然得到相同的错误。
NSMutableArray
没有
setObject:atIndex:
,而是
replaceObjectAtIndex:withObject:
。道歉,在跑出家门的时候贴出来,并编了一个方法。我已经纠正了,解释了我头脑中混为一谈的两种方法。