NSPredicate 字符串与 NSSet 匹配

发布于 2024-09-12 17:13:34 字数 904 浏览 9 评论 0原文

我正在尝试使用 NSPredicate 来搜索与生成的 CoreData 对象数组相匹配的字符串,如下所示:

// Grab an array of all Company projects
NSArray *array = [[company projects] allObjects];

我使用以下谓词将任何项目名称、公司名称或客户名称与不区分大小写的字符串匹配(注意:这应该允许部分匹配,以便“App”将匹配“Apple Inc.”等):

(name LIKE[cd] %@) OR (ANY companies.name LIKE[cd] %@) OR (ANY companies.clients.name LIKE[cd] %@)

谓词中提到的 CoreData 关系如下所示:

SELF -> (NSString *) name
SELF -> (NSSet *) companies -> (NSString *) name
SELF -> (NSSet *) companies -> (NSSet *) -> clients -> (NSString *) name

每当我尝试按上述谓词进行过滤时,我都会在控制台中找到以下内容:

HIToolbox: ignoring exception 'Can't do regex matching on object {(
    "Apple Inc.",
"Test Co.",
Microsoft
)}.'

如果我理解正确的话,看起来好像尝试匹配键路径“companies.clients.name”会返回一个 NSSet (或其他对象),其中需要 NSString (名称)。

我做错了什么?

I'm trying to use an NSPredicate to search for string matches against an array of CoreData objects generated like so:

// Grab an array of all Company projects
NSArray *array = [[company projects] allObjects];

I use the following predicate to match any project names, company names or client names with a case-insensitive string (note: this should allow for partial matches, so that 'App' will match 'Apple Inc.', etc):

(name LIKE[cd] %@) OR (ANY companies.name LIKE[cd] %@) OR (ANY companies.clients.name LIKE[cd] %@)

The CoreData relationships mentioned in the predicate look like so:

SELF -> (NSString *) name
SELF -> (NSSet *) companies -> (NSString *) name
SELF -> (NSSet *) companies -> (NSSet *) -> clients -> (NSString *) name

Whenever I attempt to filter by the above predicate, I find the following in my Console:

HIToolbox: ignoring exception 'Can't do regex matching on object {(
    "Apple Inc.",
"Test Co.",
Microsoft
)}.'

If I'm understanding things correctly, it looks as though trying to match against the keypath "companies.clients.name" returns an NSSet (or other object) where an NSString (name) was expected.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-09-19 17:13:34

companies.clients.name 将返回 NSSet 的集合,其中集合中的每个元素都是一个公司的客户名称的内容(即一个 NSSet >)。您可能想要使用 [电子邮件受保护]在你的谓词字符串中。

请参阅使用指南 键值编码中的集合和数组运算符

companies.clients.name will return collection of NSSets, where each element in the colletion is the contents of one companie's client's names (i.e. an NSSet). You probably want to use [email protected] in your predicate string.

See the guide on using Set and Array Operators in Key-Value Coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文