NSPredicate 表达式中 SUBQUERY 的快速解释

发布于 2024-09-25 08:33:55 字数 192 浏览 9 评论 0原文

关于 Apple 的 SUBQUERY 关键字的文档似乎为零,我在 SO 或 Google 上找不到关于它的简单解释。这是一个阴谋! ;)

拜托,内部圈子里的人可以快速解释一下它的语法,以便我可以使用它吗?

SUBQUERY(Bs, $x, $x IN %@)

谢谢

There appears to be zero documentation about the SUBQUERY keyword from Apple and I can't find a simple explanation about it on SO or on Google. It's a conspiracy! ;)

Please, could someone from the inner-circle please just provide a quick explanation of its syntax so I can use it?

SUBQUERY(Bs, $x, $x IN %@)

Thanks

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

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

发布评论

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

评论(3

屋顶上的小猫咪 2024-10-02 08:33:55

对于不太明白文档内容的人来说,SUBQUERY 本质上是这样的:

SUBQUERY(collection, variableName, predicateFormat)

并且可以(简单地)像这样实现:

id resultingCollection = ...; //a new collection, either a mutable set or array
NSMutableDictionary * substitutions = [NSMutableDictionary dictionary];
NSPredicate * p = [NSPredicate predicateWithFormat:predicateFormat];
for (id variable in collection) {
  [substitutions setObject:variable forKey:variableName];
  NSPredicate * filter = [p predicateWithSubstitutionVariables:substitutions];
  if ([filter evaluateWithObject:collection] == YES) {
    [resultingCollection addObject:variable];
  }
}
return resultingCollection;

所以简而言之,SUBQUERY code> 基本上是获取一个对象集合,并根据 SUBQUERY 的谓词表达式过滤出各种对象,然后返回结果集合。 (谓词本身可以包含其他 SUBQUERY

示例:

NSArray * arrayOfArrays = [NSArray arrayWithObjects:
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           nil];
NSPredicate * filter = [NSPredicate predicateWithFormat:@"SUBQUERY(SELF, $a, $a.@count > 42)"];
NSArray * filtered = [arrayOfArrays filteredArrayUsingPredicate:filter];
//"filtered" is an array of arrays
//the only arrays in "filtered" will have at least 42 elements each

And for people who don't quite get what the documentation is saying, a SUBQUERY is essentially this:

SUBQUERY(collection, variableName, predicateFormat)

And could (simplistically) be implemented like this:

id resultingCollection = ...; //a new collection, either a mutable set or array
NSMutableDictionary * substitutions = [NSMutableDictionary dictionary];
NSPredicate * p = [NSPredicate predicateWithFormat:predicateFormat];
for (id variable in collection) {
  [substitutions setObject:variable forKey:variableName];
  NSPredicate * filter = [p predicateWithSubstitutionVariables:substitutions];
  if ([filter evaluateWithObject:collection] == YES) {
    [resultingCollection addObject:variable];
  }
}
return resultingCollection;

So in a nutshell, a SUBQUERY is basically taking a collection of objects and filtering out various objects based on the predicate expression of the SUBQUERY, and returning the resulting collection. (And the predicate itself can contain other SUBQUERYs)

Example:

NSArray * arrayOfArrays = [NSArray arrayWithObjects:
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           [NSArray arrayWithObjects:....],
                           nil];
NSPredicate * filter = [NSPredicate predicateWithFormat:@"SUBQUERY(SELF, $a, $a.@count > 42)"];
NSArray * filtered = [arrayOfArrays filteredArrayUsingPredicate:filter];
//"filtered" is an array of arrays
//the only arrays in "filtered" will have at least 42 elements each
静水深流 2024-10-02 08:33:55

这是子查询的计算结果。(来自 此邮件列表线程,Google 中“NSPredicate 子查询”的排名第一的点击率。)那段文档还解释了谓词格式字符串语法如何与其相关。

This is what a subquery evaluates to. (Found from this mailing list thread, the #1 hit for “NSPredicate subquery” in Google.) That bit of documentation also explains how the predicate format string syntax relates to it.

遇到 2024-10-02 08:33:55

子查询表示一个谓词(第三个参数 - $x IN %@),该谓词对所有对象(第二个参数 - $x - 它就像 foreach 中的变量名)进行计算关系(第一个参数 - Bs)。与常规查询类似,返回对象列表。

我在很多地方看到人们几乎教条地使用 $x ,但是 objects 关系中的 $object 也非常有意义(或者 cities 中的 $city...) :)

我前段时间写了一篇关于 SUBQUERY 的博客文章。您可以在此处查看。

Subquery represents a predicate (third argument - $x IN %@) that is evaluated on all objects (second argument - $x - it's like a variable name in foreach) of a relationship (first argument - Bs). Similarly to regular query returns a list of objects.

I see in many places that people use $x almost dogmatically, but $object in objects relationship makes perfect sense as well (or $city in cities...) :)

I've written a blog post about SUBQUERY some time ago. You can check it here.

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