2

在我的应用程序中,我有一个包含大约 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]];

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

4

2 回答 2

2

我会做一些更像...

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%k like[c] %@",propertyIAmLookingFor,searchText];
于 2012-04-09T01:46:34.740 回答
0

你的书课是字符串吗?如果没有,那么你不能像 SELF 一样使用。您需要替换要比较的属性的名称。

于 2012-04-09T00:28:17.630 回答