我正在使用 NSPredicate 来过滤搜索栏中的数组。这是我的代码,可以完美运行。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[cd] %@",searchText];
filteredArray = [NSMutableArray arrayWithArray:[storesArray filteredArrayUsingPredicate:predicate]];
问题是我想添加第二个条件作为名称,即地址。我怎样才能做到这一点?我试过这个,但是所有谓词都不起作用。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[cd] %@",searchText];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF.address contains[cd] %@",searchText];
NSPredicate *fullPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate, predicate1]];
filteredArray = [NSMutableArray arrayWithArray:[storesArray filteredArrayUsingPredicate:fullPredicate]];
最佳答案
您可以使用 OR comparison operator :
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF.name contains[cd] %@ OR SELF.address contains[cd] %@", searchText, searchText];
关于ios - NSPredicate 多个条件无结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24641290/