Where to find the documentation of the SUBQUERY feature of NSPredicate for Core Data?

后端 未结 1 1200
滥情空心
滥情空心 2020-12-19 09:47

Where to find the documentation of the SUBQUERY feature of NSPredicate for Core Data?

相关标签:
1条回答
  • 2020-12-19 10:06

    Good question... it seems like this isn't well documented.

    Here is what I found:

    • 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 specify SUBQUERY 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.

    Update:

    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
      
    • You can find a more complex example in the App Extension Programming Guide > App Extension Essentials > Handling Common Scenarios section:

      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
      
    • There's also an NSPredicate Cheatsheet which discusses SUBQUERY in addition to several other NSPredicate features.


    Essentially each SUBQUERY is equivalent to filter in Swift. And ANY is equivalent to contains.

    So taking this example again:

    SUBQUERY (
        extensionItems,
        $extensionItem,
        SUBQUERY (
            $extensionItem.attachments,
            $attachment,
            ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf"
        ).@count == $extensionItem.attachments.@count
    ).@count == 1
    

    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
    
    0 讨论(0)
提交回复
热议问题