Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.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
Ios 有没有办法找到NSArray中所有匹配的对象?_Ios_Objective C - Fatal编程技术网

Ios 有没有办法找到NSArray中所有匹配的对象?

Ios 有没有办法找到NSArray中所有匹配的对象?,ios,objective-c,Ios,Objective C,如果有一个NSArray对象具有多个int值(例如@[1,5,3,8,1,1]),那么在Objective-C中是否有方法找到所有匹配对象的索引 对于1,给定上面的数组,返回1、5和6 我可以使用for循环,但如果它存在的话,我正在寻找一种更快的方法 非常感谢。首先,所有指数都是以零为基础的,因此预期指数为0、4和5 我想ObjC中最有效的方法是indexesOfObjectsPassingTest,它返回一个NSIndexSet NSArray<NSNumber *> *array

如果有一个NSArray对象具有多个int值(例如@[1,5,3,8,1,1]),那么在Objective-C中是否有方法找到所有匹配对象的索引

对于1,给定上面的数组,返回1、5和6

我可以使用for循环,但如果它存在的话,我正在寻找一种更快的方法


非常感谢。

首先,所有指数都是以零为基础的,因此预期指数为0、4和5

我想ObjC中最有效的方法是
indexesOfObjectsPassingTest
,它返回一个
NSIndexSet

NSArray<NSNumber *> *array = @[@1,@5,@3,@8,@1,@1];
NSIndexSet *indexSet = [array indexesOfObjectsPassingTest:^BOOL(NSNumber *obj, NSUInteger idx, BOOL * _Nonnull stop) {
    return [obj isEqualToNumber:@1];
}];
NSLog(@"%@", indexSet);
NSArray*array=@[@1,5,3,8,1,1];
NSIndexSet*indexSet=[array indexesOfObjectsPassingTest:^BOOL(NSNumber*obj,NSUInteger idx,BOOL*\u非空停止){
返回[obj isEqualToNumber:@1];
}];
NSLog(@“%@”,索引集);

首先,所有索引都是以零为基础的,因此预期的索引是0、4和5

我想ObjC中最有效的方法是
indexesOfObjectsPassingTest
,它返回一个
NSIndexSet

NSArray<NSNumber *> *array = @[@1,@5,@3,@8,@1,@1];
NSIndexSet *indexSet = [array indexesOfObjectsPassingTest:^BOOL(NSNumber *obj, NSUInteger idx, BOOL * _Nonnull stop) {
    return [obj isEqualToNumber:@1];
}];
NSLog(@"%@", indexSet);
NSArray*array=@[@1,5,3,8,1,1];
NSIndexSet*indexSet=[array indexesOfObjectsPassingTest:^BOOL(NSNumber*obj,NSUInteger idx,BOOL*\u非空停止){
返回[obj isEqualToNumber:@1];
}];
NSLog(@“%@”,索引集);

你会得到0、4、5。我怀疑有没有比遍历数组更快的方法。如果要处理大型数组,可以先执行[array contains]以确保至少有一个匹配元素,然后返回0、4、5。我怀疑有没有比遍历数组更快的方法。如果你在处理大型数组,你可以先做一个[array contains],以确保至少有一个匹配的元素。哦,是的!非常感谢!哦,是的!非常感谢!