ios - NSPredicate 多个条件无结果

标签 ios objective-c

我正在使用 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/

相关文章:

ios - X.01 中的 iPhone 应用程序版本号

iphone - 将 UIImage 转换为 PDF 时应用程序崩溃

ios - Restkit 发布不发送对象

ios - UIView 在没有 UIViewController 的情况下获取宽度/高度

IOS 内存错误,而 DELETE 按钮显示在 TableView 上

ios - 用于正则表达式模式匹配崩溃的 NSPredicate

ios - 我们可以在 cordova 中识别 UIGesture(在 cordova 中显示 native 侧边菜单的 native 手势)

iphone - UIControl 未接收到触摸

ios - SKSpriteNode 的动画旋转

ios - 如何使用 SDWebImage 从内存缓存中获取图像?