OGeek|极客世界-中国程序员成长平台

标题: iphone - 对象和 NSPredicate [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 22:27
标题: iphone - 对象和 NSPredicate

在我的应用程序中,我有一个包含大约 12000 个条目的大表。我在tableview上显示它。但是在进行动态搜索时搜索栏太慢了。我读过 NSPredicate 方法比 NSRange 更持久。

这是我的旧代码:

[self.filteredListContent removeAllObjects];
listContent = [[NSArray alloc] initWithArray:[dbAccess getAllBooks]];

for (Book *book in listContent)
{

    NSRange range = [book.textBook rangeOfString:searchText options:NSCaseInsensitiveSearch];

    if (range.location != NSNotFound) 
    {
        [self.filteredListContent addObject:book];
    }
 }

我的新代码:

 [self.filteredListContent removeAllObjects];
 listContent = [[NSArray alloc] initWithArray:[dbAccess getAllBooks]];

 NSPredicate *predicate = [NSPredicate predicateWithFormat"SELF like[c] %@",searchText];

 [self.filteredListContent addObject:[listContent filteredArrayUsingPredicate:predicate]];

当我尝试执行此代码时收到此错误:“无法对对象进行正则表达式匹配。”



Best Answer-推荐答案


我会做一些更像......

NSPredicate *predicate = [NSPredicate predicateWithFormat"%k like[c] %@",propertyIAmLookingFor,searchText];

关于iphone - 对象和 NSPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067014/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4