NSPredicate for property of object in NSArray of NSArray
I have an objects like the below structure: Transaction has an array of Items. Item has an array of 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
I would like to create predicate to find name and price of Subitem from NSArray of Transaction
pred = [NSPredicate predicateWithFormat:
@"ANY SELF.%K.%K.%K contains %@",
@"items",
@"subitems",
@"name",
@"MY_SUB_ITEM_NAME"];
This generates the error below.
failed: caught "NSInvalidArgumentException", "Can't do regex matching on object ( MY_SUB_ITEM_NAME )."
However when I use the similar format to find out properties in Transaction. It works fine.
pred = [NSPredicate predicateWithFormat:
@"SELF.%K LIKE %@",
@"identifier",
@"TX001"];
How should I correct my predicate to query property in Item and SubItem
I think that cannot work with -predicateWithFormat:
for two aggregation levels. On each level you can have a ANY
or an ALL
aggregation. So the "right" syntax would be ANY items ANY subitems.… = …
. Or ANY items ALL subitems.…= …"
or …
You can try to build the predicate manually with NSExpression
, probably using subqueries.
Oh, you are not using Core Data, what I assumed. So simply do it manually in a loop or use a computed property.
链接地址: http://www.djcxy.com/p/65004.html上一篇: 优化x64汇编器MUL循环