[英]NSPredicate for property of object in NSArray of NSArray
我有一个类似于以下结构的对象:事务有一个项目数组。 项目有一个子项目数组
@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.properties 中的属性时。 它工作正常。
pred = [NSPredicate predicateWithFormat:
@"SELF.%K LIKE %@",
@"identifier",
@"TX001"];
我应该如何更正谓词以查询Item和SubItem 中的属性
我认为这不能与-predicateWithFormat:
用于两个聚合级别。 在每个级别上,您都可以有一个ANY
或一个ALL
聚合。 所以“正确”的语法是ANY items ANY subitems.… = …
。 或ANY items ALL subitems.…= …"
或…
您可以尝试使用NSExpression
手动构建谓词,可能使用子查询。
哦,我假设您没有使用 Core Data。 因此,只需在循环中手动执行或使用计算属性即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.