我有一个类似下面结构的对象:Transaction有一个Items数组。 Item有一个SubItems数组
@interface Transaction : NSObject
@property (nonatomic, copy) NSString *id;
@property (nonatomic, assign) NSInteger status;
@property (nonatomic, strong) NSArray *items; // array of Item
@end
@interface Item : NSObject
@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, assign) NSInteger price;
@property (nonatomic, strong) NSArray *subitems;
@end
@interface SubItem : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger price;
@end
我想创建谓词,以便从 NSArray of Transaction 中找到名称和价格 Subitem
pred = [NSPredicate predicateWithFormat:
@"ANY SELF.%K.%K.%K contains %@",
@"items",
@"subitems",
@"name",
@"MY_SUB_ITEM_NAME"];
这会产生以下错误。
失败:捕获" NSInvalidArgumentException","无法进行正则表达式匹配 对象( MY_SUB_ITEM_NAME)。"
然而,当我使用类似的格式来查找Transaction中的属性时。它工作正常。
pred = [NSPredicate predicateWithFormat:
@"SELF.%K LIKE %@",
@"identifier",
@"TX001"];
如何更正谓词以查询项和子项
中的属性答案 0 :(得分:1)
我认为对于两个聚合级别,-predicateWithFormat:
无效。在每个级别,您可以拥有ANY
或ALL
聚合。所以"对"语法为ANY items ANY subitems.… = …
。或ANY items ALL subitems.…= …"
或......
您可以尝试使用NSExpression
手动构建谓词,可能使用子查询。
哦,你没有使用Core Data,我的假设。因此,只需在循环中手动执行或使用计算属性。