Iphone 从NSMutableArray中删除奇偶行
我们可以从NSMutable数组中每隔一行(奇数/偶数方式)删除一行吗?您可以使用NSMutable数组的-RemoveObjectsIndex:一次删除特定索引中的所有对象:Iphone 从NSMutableArray中删除奇偶行,iphone,objective-c,nsmutablearray,Iphone,Objective C,Nsmutablearray,我们可以从NSMutable数组中每隔一行(奇数/偶数方式)删除一行吗?您可以使用NSMutable数组的-RemoveObjectsIndex:一次删除特定索引中的所有对象: NSMutableArray *array = ...; NSMutableIndexSet *indexes = [[[NSMutableIndexSet alloc] init] autorelease]; // Some loop to populate 'indexes' with the indices of
NSMutableArray *array = ...;
NSMutableIndexSet *indexes = [[[NSMutableIndexSet alloc] init] autorelease];
// Some loop to populate 'indexes' with the indices of the odd/even items
[array removeObjectsAtIndexes:indexes];
是的,我正在使用for循环——我的意思是,有任何现成的函数或命令吗?没有,你必须手工构造一个NSIndexSet。如果使用不带NSIndexSet的循环按顺序删除项,您将发现无法删除正确的项(删除一项后,其他项的索引将发生更改)。
newArray=[array objectsAtIndexes:[array indexesOfObjectsPassingTest:^BOOL(id obj,NSUInteger index,BOOL*stop){return(index&1) == 0 ; } ] ] ;代码>