问题描述
在哪里可以找到 NSPredicate for Core Data 的 SUBQUERY 特性的文档?
Where to find the documentation of the SUBQUERY feature of NSPredicate for Core Data?
推荐答案
好问题……这似乎没有很好的文档记录.
Good question... it seems like this isn't well documented.
这是我发现的:
- 可以在NSExpression(forSubquery:usingIteratorVariable:predicate:) 文档.它包含两个示例和语法:
SUBQUERY(collection_expression, variable_expression, predicate);
- NSExpression简要提及 Subquery Expressions 和
NSSubqueryExpressionType
,这是您在谓词中指定SUBQUERY
时使用的内容. - 找到文档最合乎逻辑的地方是 Predicate Programming指南,但只是提到了几次;在 字符串比较 和 保留字部分.
- 您可以尝试使用 Google 搜索受限到 apple.com,但这只会返回 53 个结果.
- The most documentation can be found at NSExpression(forSubquery:usingIteratorVariable:predicate:) documentation. It contains two examples and the syntax:
SUBQUERY(collection_expression, variable_expression, predicate);
- NSExpression briefly mentions Subquery Expressions and the
NSSubqueryExpressionType
, which is what is used when you specifySUBQUERY
in your predicate. - The most logical place to find the documentation would be the Predicate Programming Guide, but it is just mentioned a few times; in the String Comparisons, and the Reserved Words sections.
- You can try a Google search limited to apple.com, but this only returns 53 results.
更新:
通过添加 App Extensions,Apple 包含了更多 SUBQUERY 示例,因为它们是复杂匹配逻辑所必需的.
With the addition of App Extensions, Apple has included more SUBQUERY examples since they are required for complex matching logic.
In the String Comparisons section of the Predicate Programming Guide, it now includes an example of how to match a UTI:
SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf"
).@count == $extensionItem.attachments.@count
).@count == 1
你可以在 App Extension Programming Guide > App Extension Essentials > 处理常见场景部分:
SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "org.appextension.action-one" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "org.appextension.action-two"
).@count == $extensionItem.attachments.@count
).@count == 1
还有一个 NSPredicate Cheatsheet 讨论了 SUBQUERY 以及其他几个NSPredicate 特性.
There's also an NSPredicate Cheatsheet which discusses SUBQUERY in addition to several other NSPredicate features.
本质上每个 SUBQUERY
都相当于 Swift 中的 filter
.而ANY
等价于contains
.
Essentially each SUBQUERY
is equivalent to filter
in Swift. And ANY
is equivalent to contains
.
再拿这个例子:
SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf"
).@count == $extensionItem.attachments.@count
).@count == 1
这与 Swift 中的类似:
It would be similar to this in Swift:
extensionItems.filter {
$0.attachments.filter {
$0.registeredTypeIdentifiers.contains {
$0.utiConformsTo("com.adobe.pdf")
}
}.count == $0.attachments.count
}.count == 1
这篇关于在哪里可以找到 NSPredicate for Core Data 的 SUBQUERY 功能的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!