NSArray的NSArray中的对象的属性的NSPredicate
我有一个像下面的结构一样的对象: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中找到Subitem的名称和价格
pred = [NSPredicate predicateWithFormat:
                    @"ANY SELF.%K.%K.%K contains %@",
                    @"items",
                    @"subitems",
                    @"name",
                    @"MY_SUB_ITEM_NAME"];
这会在下面产生错误。
失败:发现“NSInvalidArgumentException”,“无法在对象(MY_SUB_ITEM_NAME)上执行正则表达式匹配”。
但是,当我使用类似的格式来查找交易中的属性。 它工作正常。
 pred = [NSPredicate predicateWithFormat:
                    @"SELF.%K LIKE %@",
                    @"identifier",
                    @"TX001"];
我应该如何更正我的谓词以查询Item和SubItem中的属性
  我认为这不能用于两个聚合级别的-predicateWithFormat:  在每个级别上,您都可以拥有ANY或ALL聚合。  因此,“正确”的语法将是ANY items ANY subitems.… = …  或ANY items ALL subitems.…= …"或... 
  您可以尝试使用NSExpression手动构建谓词,可能使用子查询。 
哦,你没有使用核心数据,我认为。 因此,只需在循环中手动执行或使用计算属性即可。
链接地址: http://www.djcxy.com/p/65003.html上一篇: NSPredicate for property of object in NSArray of NSArray
