使用 Predicate 从 NSArray 中搜索 [英] Search from NSArray using Predicate

查看:29
本文介绍了使用 Predicate 从 NSArray 中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的数组如下所示.我为 sectionIndex 表创建了这个数组格式.所以我可以从数组中搜索并使用索引表显示搜索文本.

my current array looks like below. i create this array format for sectionIndex table. so i can search from array and display search text using index table.

arrContent = (
    {
    key = A;
    value =         (
        Ac,
        Acting
    );
},
    {
    key = B;
    value =         (
        Basketball,
        Baseball
    );
},
    {
    key = C;
    value =         (
        Cat
    );
}
    {
    key = P;
    value =         (
        Panda,
        Peacock
    );
}
)

我的搜索代码如下所示.并且工作正常.

My Search code looks like below. and it is working fine.

-(void)searchText:(NSString *) text
{
[arrSearch removeAllObjects];
for (int i = 0; i < [arrContent count]; i++)
{
    NSMutableArray *temp = [[NSMutableArray alloc]init];
    NSMutableArray *arrFind = arrContent[i][@"value"];
    for (int j = 0; j < [arrFind count]; j++)
    {
        NSString *str = arrFind[j];
        // containsString: method find my string if its available within range
        if ([str containsString:text])
            [temp addObject:str];
    }

    if ([temp count])
    {
        [arrSearch addObject:@{@"key":arrContent[i][@"key"],@"value":temp}];
    }
}
NSLog(@"Search : %@",text);
NSLog(@"arrSearch : %@",arrSearch);
}

我得到如下输出.这是正确的.

i got output like below. which is correct.

Search : ac
arrSearch : (
    {
    key = A;
    value =         (
        Ac,
        Acting
    );
},
    {
    key = P;
    value =         (
        Peacock
    );
}
)

我只是想问一下,是否有更好的方法来使用 NSPredicate 搜索并获得相同的输出,因为当有大量数据时,for 循环会花费时间.

I just want to ask that if is there any better way to search and get same output using NSPredicate because for loop will take time when lots of data.

我们会提供帮助.

推荐答案

你可以像这样在 NSArray 上使用内置函数...

You could use the built in function on NSArray like this...

- (NSArray *)filterArrayUsingText:(NSString *)text
{
    NSMutableArray *filteredWordArray = [NSMutableArray array];

    for (NSDictionary *dictionary in yourWordArray) {
        NSArray *filteredWords = [self filterWordsFromArray:dictionary[@"value"] usingText:text];

        if (filteredWords) {
            [filteredWordArray addObject:@{@"key":dictionary[@"key"], @"value", filteredWords}];
        }
    }

    return filteredWordArray;
}

- (NSArray *)filterWordsFromArray:(NSArray *)wordArray usingText:(NSString *)text
{
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^(NSString *theWord, NSDictionary *bindings) {
        return [theWord containsString:text];
    }];

    NSArray *filteredArray = [wordArray filteredArrayUsingPredicate:predicate];

    return filteredArray.count == 0 ? nil : filteredArray;
}

另外,请确保使用现代 Obj-C 语法.

Also, make sure to use modern Obj-C syntax.

此外,数据的存储方式也有问题.对于键/值对,您不存储 KeyValue.

Also, the way the data is stored is a bit broken. For key/value pairs you don't store Key and Value.

你会这样存储...

{
    A: (Ac,
        Acting),
    B: (Basketball,
        Baseball),
    C: (Cat),
    P: (Panda,
        Peacock)
}

这篇关于使用 Predicate 从 NSArray 中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆