Is there a way to force NSMutableArray to allow you to have “holes” in the array where only parts of the array are filled, not adjacent elements?
I want to create an initial array with some elements filled at the beginning of the array. After some time, I would like to insert 9 new elements, not immediately after my last, but at some point in the middle of the array, leaving a hole in the parts of the list of arrays there.
I thought I could do this, but we get the error:
NSRangeException', reason: '***
-[NSMutableArray insertObject:atIndex:]: index 28 beyond bounds [0 .. 5]'
the code:
NSMutableArray *targetArray = [NSMutableArray arrayWithCapacity:100];
- (void)fillInInitially {
for (int i = 0; i < [initialData count]; ++i)
{
[targetArray insertObject:[initialData objectAtIndex:i] atIndex:i];
}
}
- (void)fillInTheBlank:(NSArray *) additions {
NSRange range = NSMakeRange(28, [additions count]);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
[targetArray insertObjects:additions atIndexes:indexSet];
}
I did not want to insert “empty” elements into the holes, since it takes up memory, when I can contain several hundred objects in the array.
I need some sort of data structure with a sparse array.