Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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
Objective c 在NSArray中查找字符串的所有索引_Objective C_Nsmutablearray - Fatal编程技术网

Objective c 在NSArray中查找字符串的所有索引

Objective c 在NSArray中查找字符串的所有索引,objective-c,nsmutablearray,Objective C,Nsmutablearray,我有NSMutableArray,其中的数据有:“John”、“Phillip”、“John”、“Andrea”。我还有一个字符串“John”。我需要在NSMutableArray中找到该字符串,并定义哪个索引将显示其他数组中的其他数据。在我的例子中,索引是0和2 这场演出只是第一场 NSInteger index = [array indexOfObject:String]; NSArray提供了一种方法,可根据您提供的条件生成一组索引: NSIndexSet *allPositions =

我有
NSMutableArray
,其中的数据有:“John”、“Phillip”、“John”、“Andrea”。我还有一个字符串“John”。我需要在
NSMutableArray
中找到该字符串,并定义哪个索引将显示其他数组中的其他数据。在我的例子中,索引是0和2

这场演出只是第一场

NSInteger index = [array indexOfObject:String];

NSArray
提供了一种方法,可根据您提供的条件生成一组索引:

NSIndexSet *allPositions = [array indexesOfObjectsPassingTest:
    ^BOOL (id str, NSUInteger i, BOOL *stop) {
        return [str isEqualToString:String];
    }];

这将生成包含所有感兴趣索引的
NSIndexSet
,在您的情况下,它将包含0和2。

NSArray
提供了一种方法,该方法根据您提供的条件生成一组索引:

NSIndexSet *allPositions = [array indexesOfObjectsPassingTest:
    ^BOOL (id str, NSUInteger i, BOOL *stop) {
        return [str isEqualToString:String];
    }];

这将生成具有所有感兴趣的索引的
NSIndexSet
——在您的情况下,它将具有0和2。

我应该如何在[data objectAtIndex:allPositions]中使用此allPositions?我需要整数来显示数据。@WildWorld根据您的情况,您可以使用
GetIndexRange:maxCount:inIndexRange:
方法将其转换为
NSInteger
s数组。我应该如何在[data ObjectatinIndex:allPositions]中使用此allPositions?我需要整数来显示数据。@WildWorld根据您的情况,您可以使用
getIndexes:maxCount:inIndexRange:
方法将其转换为
NSInteger
s数组。