Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Here is what I found:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...