ios - 如何构造多个复杂条件的NSPredicate

标签 ios nspredicate

考虑一个具有以下属性的 CustomObject 类:

@property(nonatomic, strong) NSNumber *source;
@property(nonatomic, strong) NSArray<CustomObject2> *linkedItems;
@property(nonatomic, strong) NSString *parentId;

我将如何构造一个 NSPredicate 来处理以下情况:

  1. source 值为 1 和非空/非 nil linkedItems 数组的所有 CustomObject 对象。
  2. source 值为 2parentId 等于 item1 的所有 CustomObject 对象>.
  3. 所有其他 CustomObjectssource 值不是 12

例如:

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 的文档,其中包含可以为ANDORNOT< 构造谓词的类方法 使用其他谓词作为输入的条件。

关于ios - 如何构造多个复杂条件的NSPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47825653/

相关文章:

ios - showBluetoothAccessoryPickerWithNameFilter 中的 NSPredicate

objective-c - Objective C 正则表达式以 1 或 2 位数字开头

ios - dyld : Library not loaded: @rpath/Alamofire. 框架/Alamofire

ios - 如何将 UICollectionView 单元格与 UICollectionView 边界对齐

ios - 当我实现 UITableViewRowAction 时,为什么所有节标题行都随表格单元格一起滑动

ios - 从 XML 文件 iOS 中消除重复的配置文件

xcode - 核心数据可转换属性不适用于 NSPredicate

ios - 在具有透明背景的 Alert ViewController 中心添加图像

ios - 如何在 Swift 中从 JSON 解析值大于 2147483647 的 Int

ios - Objective-c:从具有范围的集合(NSArray)中获取最短范围的最有效方法