Objective-C access / change of array elements in a multidimensional array (NSArray)

I try to change the value in a multidimensional array, but I get a compiler error:

warning: passing argument 2 of 'setValue:forKey:' makes pointer from integer without a cast

This is my content array:

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

This is how I try to change the value:

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

Decision:

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

So the array key is a string type - something strange, but useful to know.

+5
source share
4 answers
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"];

You do not want alloc+initfor auxiliary arrays, because the counter for counting sub-arrays will be too high (+1 for alloc, then +1 again, since it is inserted into an external array).

+11
source

, . NSMutableArray.

+2

NSMutableArray insertObject:atIndex:, replaceObjectAtIndex:withObject: ( , , , , ). setValue:forKey: NSString . , NSString, .

+1

1 : D
, , , , addObject

0

All Articles