views:

456

answers:

2

Hi,

I have a NSMutableArray, I want to insert data inside it, the problem is first I wanna check if the index where I'm inserting the data exist or not, but how to do that.
I try something like that but nothing is working:


if ([[eventArray objectAtIndex:j] count] == 0)

or

if (![eventArray objectAtIndex:j])

Thanks,

A: 
if (j < [eventArray count])
{
     //Insert
}
willcodejavaforfood
Doesn't answer the OP's question; ludo is looking for something akin to a sparse array.
bbum
I'm stupid sometimes -,.- anyway thanks for this fast answer~ ^^
ludo
no no it's working perfectly, I didn't even think of that easy one haha
ludo
@ludo - No problem mate :)
willcodejavaforfood
Sigh. So wrong.... I look forward to the next question from OP.
bbum
@bbum - That's the spirit :)
willcodejavaforfood
+1  A: 

NSArray and NSMutableArray are not sparse arrays. Thus, there is no concept of "exists at index", only "does the array have N elements or more".

For NSMutableArray, the grand sum total of mutable operations are:

- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

All other mutability methods can be expressed in terms of the above and -- more specifically to your question -- removing an object does not create a hole (nor can you create an array with N "holes" to be filled later).

bbum
This does not answer the OP's question :P
willcodejavaforfood