Using "replaceObjectAtIndex" for an array in an array

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

I 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 information [5145: 207] * Application terminated due to an uncaught exception "NSInvalidArgumentException", reason: '- [__ NSArrayI replaceObjectAtIndex: withObject:]: unrecognized selector sent to instance 0x4e24d70 '

I tried this too:

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

but I get the following error:

* Application termination due to an uncaught exception "NSRangeException", reason: "* - [NSMutableArray replaceObjectAtIndex: withObject:]: index 16660 is out of bounds [0 .. 0] '

+4
source share
2 answers

The second approach is based on using 3 arrays instead of 2. The 1st approach seems fine, but I think you have NSArray inside NSMutableArray , because NSArray:replaceObjectAtIndex:withObject does not exist. Therefore, make sure that all arrays are mutable.

+4
source

So, as I understand it, you have: a mutable array, in which case you have more arrays. Now you want to get one of these "subarrays" and change it.

In this case, the first attempt is correct, except that you have NSArray instances inside your NSMutableArray . And you cannot change them, therefore, an exception. Therefore, you need to make sure that you NSMutableArrays inside the external NSMutableArray . Then the challenge of your first attempt will be successful.

+2
source

Source: https://habr.com/ru/post/1344193/


All Articles