• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - NSArray 的 NSArray 中对象属性的 NSPredicate

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:08:52 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个类似以下结构的对象: 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 中查找 Subitemnameprice p>

pred = [NSPredicate predicateWithFormat:
                    @"ANY SELF.%K.%K.%K contains %@",
                    @"items",
                    @"subitems",
                    @"name",
                    @"MY_SUB_ITEM_NAME"];

这会产生以下错误。

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

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

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

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

中的属性



Best Answer-推荐答案


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

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

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

关于ios - NSArray 的 NSArray 中对象属性的 NSPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32263367/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap