How to add an object in a random position of an NSMutableArray?

2

I am creating an array with the following capacity 15 in this way:

meuArray = [[NSMutableArray alloc] initWithCapacity:15];

But when I try to insert an object initially in position 10 like this:

[meuArray insertObject:algumObjeto atIndex:10];

I get an error saying that I have exceeded the size of the array:

  

* Terminating app due to uncaught exception 'NSRangeException', reason: '* - [__ NSArrayM insertObject: atIndex:]: index 10 beyond   bounds for empty array '

How could I solve this?

    
asked by anonymous 27.03.2015 / 16:56

1 answer

3

When you initialize NSMutableArray with a certain capacity, this is used more as a "hint" for the class of how many elements you think to store (it is the initial number of objects that the array can store, but it will expand if necessary). But the array is still empty after calling alloc / init , which is why you can not insert an object in position 10.

    
27.03.2015 / 17:17