我需要查詢所有count>10的ClassA的bObjects中name為hello的ClassB的對象。請問能不能用一個(gè)NSPredicate來表示這個(gè)查詢?
2 回答

精慕HU
TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
看了蘋果的文檔,里面寫的例子:
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'"];
所以,我覺得查詢應(yīng)該是:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name = %@) AND (ANY aObjects.count > 10)", @"hello"];

阿波羅的戰(zhàn)車
TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
腦補(bǔ)的,沒測試。不好用請更正我
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 關(guān)注
- 153 瀏覽
添加回答
舉報(bào)
0/150
提交
取消