NSCompoundPredicate [英] NSCompoundPredicate

查看:29
本文介绍了NSCompoundPredicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UISearchDisplayControllerNSCompoundPredicate 过滤 UITableView's 数据.我有一个带有 3 个 UILabels 的自定义单元格,我希望在搜索中全部过滤,因此是 NSCompoundPredicate.

I'm trying to filter a UITableView's data using a UISearchDisplayController and NSCompoundPredicate. I have a custom cell with 3 UILabels that I want to all be filtered within the search, hence the NSCompoundPredicate.

  // Filter the array using NSPredicate(s)

  NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"SELF.productName contains[c] %@", searchText];
  NSPredicate *predicateManufacturer = [NSPredicate predicateWithFormat:@"SELF.productManufacturer contains[c] %@", searchText];
  NSPredicate *predicateNumber = [NSPredicate predicateWithFormat:@"SELF.numberOfDocuments contains[c] %@",searchText];

  // Add the predicates to the NSArray

  NSArray *subPredicates = [[NSArray alloc] initWithObjects:predicateName, predicateManufacturer, predicateNumber, nil];

  NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];

但是,当我这样做时,编译器会警告我:

However, when I do this, the compiler warns me:

不兼容的指针类型初始化 'NSCompoundPredicate *_strong'带有'NSPredicate *'类型的表达式

Incompatible pointer types initializing 'NSCompoundPredicate *_strong' with an expression of type 'NSPredicate *'

我在网上看到的每个例子都做同样的事情,所以我很困惑.NSCompoundPredicate orPredicateWithSubpredicates: 方法在最后一个参数中使用了 (NSArray *),所以我真的很困惑.

Every example I've seen online does this exact same thing, so I'm confused. The NSCompoundPredicate orPredicateWithSubpredicates: method takes an (NSArray *) in the last parameter, so I'm REALLY confused.

怎么了?

推荐答案

首先,使用contains"很慢,考虑一下beginswith"?其次,你想要的是:

First of all, using "contains" is very slow, consider mayber "beginswith"? Second, what you want is:

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];

三,你可以这样做:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.productName beginswith[cd] %@ OR SELF.productManufacturer contains[cd] %@", searchText, searchText];

这篇关于NSCompoundPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆