考虑一个具有以下属性的 CustomObject
类:
@property(nonatomic, strong) NSNumber *source;
@property(nonatomic, strong) NSArray<CustomObject2> *linkedItems;
@property(nonatomic, strong) NSString *parentId;
我将如何构造一个 NSPredicate
来处理以下情况:
source
值为1
和非空/非 nillinkedItems
数组的所有CustomObject
对象。source
值为2
且parentId
等于item1
的所有CustomObject
对象>.- 所有其他
CustomObjects
的source
值不是1
或2
例如:
Custom Object 1
source = 1
linkedItems = Custom Object2 1, CustomObject2 2
parentId = nil
Custom Object 2
source = 1
linkedItems = nil
parentId = nil
Custom Object 3
source = 2
linkedItems = nil
parentId = item1
Custom Object 4
source = 2
linkedItems = nil
parentId = item2
Custom Object 5
source = 3
linkedItems = Custom Object2 3
parentId = nil
使用谓词后,我想要对象 1、2 和 5。
我很难找到一个优雅的解决方案...有什么想法吗?
最佳答案
查找NSCompoundPredicate
的文档,其中包含可以为AND
、OR
和NOT< 构造谓词的类方法
使用其他谓词作为输入的条件。
关于ios - 如何构造多个复杂条件的NSPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47825653/