2011-08-02 90 views
1

我有以下代码nspredicate iphone问题

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"Word== %@", randomWord]; 
rows = [[courseArray filteredArrayUsingPredicate:predicate]retain]; 

我在搜索栏使用此。我有两个问题 - 我如何制作NSPredicate,以便每个单词都以输入的字符开头,而不是相等的值。例如我在阵列中有“Men,Many,Morons,Now,Never”,当我输入“M”时,我想要添加“Men,Many,Morons”。此外,我如何预测超过1个值?所以它比较两种格式?

感谢...

回答

1

尝试 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Word BEGINSWITH %@", randomWord];

如果你要搜索整个单词,你可以尝试CONTAINS ..

1

NSPredicate文档是一个良好的开端。

谓词来检查字符串以另一种是[NSPredicate predicateWithFormat:@"Word BEGINSWITH %@"]

并尽可能检查两个谓词,你要找的NSCompoundPredicate

1

您需要创建所有要比较的谓词并将所有谓词附加到数组。之后,您可以使用NSCompoundPredicateor,and条件。

NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"is_neutered = 0"], 
    [NSCompoundPredicate orPredicateWithSubPredicates:feedDays]]];