本文介绍了从NS predicate在NSArray的索引obj的Ç-get名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有车的数组,我根据包含字母对象的过滤它 I

I have an array of car and I am filtering it based on objects containing the letter i.

NSMutableArray *cars = [NSMutableArray arrayWithObjects:@"Maruthi",@"Hyundai", @"Ford", @"Benz", @"BMW",@"Toyota",nil];
NSString *stringToSearch = @"i";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",stringToSearch]; // if you need case sensitive search avoid '[c]' in the predicate
NSArray *results = [cars filteredArrayUsingPredicate:predicate];

结果包含 Maruthi,现代汽车。相反的元素,我想结果来包含元素的索引即 0.1

results contains MarutHyundai. Instead of the elements, I want results to contain the indexes of the elements i.e 0,1.

推荐答案

为什么不使用

- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

或者相似?

根据你的搜索条件,这样的事情吧?

Depending on your search criteria, something like this perhaps?

    NSArray *array = @[@"Maruthi",@"Hyundai", @"Ford", @"Benz", @"BMW", @"Toyota"];
    NSString *stringToSearch = @"i";
    NSIndexSet *set = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
       NSString *string = obj;
       if (([string rangeOfString:stringToSearch].location == NSNotFound))
       {
           return NO;
       }
       return YES;
    }];

这篇关于从NS predicate在NSArray的索引obj的Ç-get名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:06