NSPredicate 用于 NSArray 的 NSArray 中对象的属性 [英] NSPredicate for property of object in NSArray of NSArray

查看:47
本文介绍了NSPredicate 用于 NSArray 的 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

我想创建谓词以从 NSArray of Transaction 中查找 Subitemnameprice>

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"];

这会产生以下错误.

failed: 捕获 "NSInvalidArgumentException", "Can't do regex matching在对象上(MY_SUB_ITEM_NAME)."

failed: caught "NSInvalidArgumentException", "Can't do regex matching on object ( MY_SUB_ITEM_NAME )."

但是当我使用类似的格式来查找 Transaction.properties 中的属性时.它工作正常.

However when I use the similar format to find out properties in Transaction. It works fine.

 pred = [NSPredicate predicateWithFormat:
                    @"SELF.%K LIKE %@",
                    @"identifier",
                    @"TX001"];

我应该如何更正谓词以查询 ItemSubItem

How should I correct my predicate to query property in Item and SubItem

推荐答案

我认为这不能与 -predicateWithFormat: 一起用于两个聚合级别.在每个级别上,您可以有一个 ANYALL 聚合.所以正确"的语法应该是 ANY items ANY subitems.... = ....或 ANY items ALL subitems.…= …" 或 …

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 …

您可以尝试使用 NSExpression 手动构建谓词,可能使用子查询.

You can try to build the predicate manually with NSExpression, probably using subqueries.

哦,你没有使用 Core Data,这是我的假设.因此,只需在循环中手动执行或使用计算属性即可.

Oh, you are not using Core Data, what I assumed. So simply do it manually in a loop or use a computed property.

这篇关于NSPredicate 用于 NSArray 的 NSArray 中对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆