开发者

Check whether data is present at an index in NSMutableArray

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

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

or

if (![eventArray objectAtInd开发者_JAVA百科ex:j])


if (j < [eventArray count])
{
     //Insert
}


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).


I've given a brief implementation of a sparse array in this question: Sparse Array answer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜