5

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

1
  • did you check to make sure an object existed at index 0 in the array? Commented Mar 17, 2011 at 21:10

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

BINGO. Thanks Kay. I had added an array within the mutable array. I assumed that doing so made all the objects mutable (duh). Works like a charm now :)
2

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.

1 Comment

Thanks DarkDust. Great advice. I had an array with the mutable. All working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.