Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "replaceObjectAtIndex" for array within array

I have an array within an (mutable) array. I am trying to replace certain objects with "replaceObjectAtIndex."

I have tried:

[[mutableArrayName objectAtIndex:0]replaceObjectAtIndex:0 withObject:@"TEST"]; 

but I get the following error:

-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x4e24d70 2011-03-17 17:02:07.008 Contact details[5145:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x4e24d70'

I tried this as well:

    [mutableArrayName replaceObjectAtIndex:[[mutableArrayName objectAtIndex:0]objectAtIndex:0] withObject:@"TEST"];

but I get the following error:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray replaceObjectAtIndex:withObject:]: index 16660 beyond bounds [0 .. 0]'

like image 710
Jeremy Avatar asked Mar 17 '11 21:03

Jeremy


2 Answers

The 2nd approach relies on having 3 arrays instead of 2. 1st approach seems to be fine but I guess you have an NSArray inside of an NSMutableArray because NSArray:replaceObjectAtIndex:withObject does not exist. So ensure that all arrays are mutable.

like image 169
Kay Avatar answered Nov 09 '22 13:11

Kay


So as far as I've understood, you have: a mutable array, and within that, you've got more arrays. Now you want to fetch one of those "sub-arrays" and modify it.

In this case the first try is the correct one, except that you've got NSArray instances inside your NSMutableArray. And you cannot modify those, hence the exception. So you need to make sure you're stuffing NSMutableArrays inside your outer NSMutableArray. Then the call of your first try will succeed.

like image 25
DarkDust Avatar answered Nov 09 '22 13:11

DarkDust