在我的应用程序中,我有一个包含大约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]];当我尝试执行这段代码时,我收到了这个错误:“无法在对象上执行正则表达式匹配。”
发布于 2012-04-09 09:46:35
我会做一些更像是..。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%k like[c] %@",propertyIAmLookingFor,searchText];发布于 2012-04-09 08:28:18
你的book类是一个字符串吗?如果没有,那么你就不能使用SELF like。您需要替换要比较的属性的名称。
https://stackoverflow.com/questions/10067014
复制相似问题