views:

1038

answers:

3

I'm trying to change a value in a multidimensional array but getting 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'm trying to change the value:

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

Solution:

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

So the array key is a string type - kinda strange but good to know.

+2  A: 

You're creating immutable arrays, and trying to change the values stored in them. Use NSMutableArray instead.

NSResponder
It doesn't make any difference. Still get the same error.
dan
A: 

You want either NSMutableArray's insertObject:atIndex: or replaceObjectAtIndex:withObject: (the former will push the existing element back if one already exists, while the latter will replace it but doesn't work for indices that aren't already occupied). The message setValue:forKey: takes a value type for its first argument and an NSString for its second. You're passing an integer rather than an NSString, which is never valid.

Chuck
There is no message like setObject:atIndex: but setObject:forKey:. But still get the same error.
dan
There is no `setObject:atIndex:` for `NSMutableArray`, instead there is `replaceObjectAtIndex:withObject:`. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
dreamlax
Apologies, posted while running out the door and made up a method. I've corrected to explain the two methods that I was conflating in my head.
Chuck
+2  A: 
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 don't want to alloc+init for the sub-arrays because the retain count of the sub-arrays will be too high (+1 for the alloc, then +1 again as it is inserted into the outer array).

dreamlax
Thanks for the hint.
dan