Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从NSMutableArray中删除奇偶行_Iphone_Objective C_Nsmutablearray - Fatal编程技术网

Iphone 从NSMutableArray中删除奇偶行

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

我们可以从NSMutable数组中每隔一行(奇数/偶数方式)删除一行吗?

您可以使用NSMutable数组的-RemoveObjectsIndex:一次删除特定索引中的所有对象:

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 ; } ] ] ;