我对此感到困惑:我已经在 iPhone Simulator 4.3 和 5.0 中测试了我的应用程序的过滤器功能,一切正常,但在 iPhone 上,谓词让我得到了错误的结果。(我怀疑它与正则表达式有关,但我没有看到错误。)
if (!selectionDidChange)
return;
[matches release];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"meta LIKE[c] %@",
UniversalKeyword];
NSPredicate *regional = [NSPredicate predicateWithFormat:@"regional == NIL OR regional == NO OR "
@"(regional == YES AND title.%K != NIL)", CurrentLanguage];
NSPredicate *exclusive = (exclusiveUpgrade ? [NSPredicate predicateWithValue:YES] :
[NSPredicate predicateWithFormat:@"exclusive == NIL OR exclusive == NO"]);
NSMutableArray *predicates = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
for (int i = 0; i < L(keywords); i++)
{
int selection = [D(A(keywords, i), @"selection") intValue];
if (selection >= 0)
A_ADD(predicates, ([NSPredicate predicateWithFormat:@"meta MATCHES[c] %@",
S(S(@".*\\b", D(A(D(A(keywords, i), @"values"), selection), @"name")), @"\\b.*")]));
}
NSPredicate *compound = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
[predicates removeAllObjects];
[predicates addObject:predicate];
[predicates addObject:compound];
predicate = [NSCompoundPredicate andPredicateWithSubpredicates:A_NEW(regional, exclusive,
[NSCompoundPredicate orPredicateWithSubpredicates:predicates])];
matches = [[entries filteredArrayUsingPredicate:predicate] retain];
selectionDidChange = NO;
为了澄清:
entries
并且keywords
是字典数组,虽然keywords
有点复杂。重要的是,其中的每个字典都entries
包含一个名为的字符串,其名称meta
可能类似于:“A、B、C、D”。如果用户搜索“C”,则正则表达式应该匹配。还有其他标准似乎不是问题,因为我检查了编译的谓词,它看起来很好。- 我应该提一下,谓词 ( ) 的第一部分也
meta LIKE[c] %@
给了我在 iPhone 上的预期结果。 - 我在这里有一些使用过的便利宏:
A_ADD = addObject:
,D = objectForKey:
,A = objectAtIndex:
,A_NEW = arrayWithObjects:
,L = count
和S = stringByAppendingString:
. (是的,我很懒:D)
我在这里俯瞰什么?