Difference between ALL , ANY and SOME operators in NSPredicate

梦想的初衷 提交于 2019-12-08 17:09:47

问题


I'm really struggling to understand these 3. It looks like ANY and SOME do the same thing, but I don't see the difference with ALL.


回答1:


Let's have a list of groups. Every group has members of type person. Every person has an age.

ALL members.age > 30

means that you will find a group with members that are all older than 30. You will not find a group with at least one single member being 30 or younger.

ANY members.age > 30

means that you will find a group with at least one member older than 30. You will not find a group with all members being 30 or younger.

Group1      > 30
  Amin  45  YES
  Chris 29  NO
            ---
All         NO  (because Chris is too young)
Any         YES (because Amin is old enough)

Group2      > 30
  Amin  45  YES
  Foo   35  YES
            ---
All         YES (because all members are old enough)
Any         YES (because at least one member is old enough)


Group3      > 30
  Chris 29  NO
  Bar   21  NO
            ---
All         NO  (because at least one member is too young)
Any         NO  (because all members are too young)

With the ALL predicate you find Group2, because all members (Amin, Foo) matches the predicate. With the ANY predicate you will find both groups, because in both groups at least one member matches the predicate.




回答2:


ALL and ANY are basic logic operators.

ALL predicate evaluates to true when predicate evaluates to true for EVERY item.

ANY predicate evaluates to true when predicate evaluates to true for at least 1 item.

Note that you can always express ALL predicate using ANY and viceversa, fore example ALL condition is equivalent to NOT (ANY NOT(condition)).

You can find more information in Predicate Programming Guide, Chapter Aggregate Operations



来源:https://stackoverflow.com/questions/30076452/difference-between-all-any-and-some-operators-in-nspredicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!