为了账号安全,请及时绑定邮箱和手机立即绑定

我有两个类,ClassA和ClassB,关系如下:

我有两个类,ClassA和ClassB,关系如下:

iOS
千万里不及你 2023-04-15 22:18:31
我需要查询所有count>10的ClassA的bObjects中name为hello的ClassB的对象。请问能不能用一个NSPredicate来表示这个查询?
查看完整描述

2 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

看了苹果的文档,里面写的例子:

If you use a to-many relationship, the construction of a predicate is slightly different. If you want to fetch Departments in which at least one of the employees has the first name "Matthew," for instance, you use an ANY operator as shown in the following example:
 NSPredicate *predicate = [NSPredicate predicateWithFormat:    @"ANY employees.firstName like 'Matthew'"];

所以,我觉得查询应该是:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name = %@) AND (ANY aObjects.count > 10)", @"hello"];


查看完整回答
反对 回复 2023-04-19
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

脑补的,没测试。不好用请更正我

NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"ClassB"
                                   inManagedObjectContext:context];NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name = %@) AND (aObjects.count > 10)", @"hello"];
[fetchRequest setPredicate:predicate];


查看完整回答
反对 回复 2023-04-19
  • 2 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信