我需要查詢所有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"];

阿波羅的戰車
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];
- 2 回答
- 0 關注
- 158 瀏覽
添加回答
舉報
0/150
提交
取消