Iphone 检查NSMutableArray中的索引中是否存在数据
我有一个Iphone 检查NSMutableArray中的索引中是否存在数据,iphone,objective-c,cocoa-touch,nsmutablearray,Iphone,Objective C,Cocoa Touch,Nsmutablearray,我有一个NSMutableArray,我想在其中插入数据,问题是首先我想检查插入数据的索引是否存在。怎么做? 我尝试过类似的方法,但没有任何效果: if ([[eventArray objectAtIndex:j] count] == 0) 或 if(j
NSMutableArray
,我想在其中插入数据,问题是首先我想检查插入数据的索引是否存在。怎么做?
我尝试过类似的方法,但没有任何效果:
if ([[eventArray objectAtIndex:j] count] == 0)
或
if(j<[eventArray count])
{
//插入
}
NSArray
和NSMutableArray
不是稀疏数组。因此,没有“索引处存在”的概念,只有“数组有N个或更多元素”
对于NSMutableArray
,可变操作的总和为:
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
所有其他可变性方法都可以用上述方式表示,更具体地说,对于您的问题,移除对象不会创建一个洞(也不能创建一个包含N个“洞”的数组,以后再填充)。在这个问题中,我给出了稀疏数组的一个简短实现:没有回答OP的问题;ludo正在寻找类似于稀疏数组的东西。我有时很愚蠢-,-无论如何,感谢这个快速的答案~^^不,它工作得很好,我甚至没有想到那个简单的hahaThis不能回答OP的问题:P
if (j < [eventArray count])
{
//Insert
}
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;