1

我将如何使用(理想情况下)带有键值编码集合运算符的单个谓词来简化这一点?(最后感兴趣的数组是filteredGames。)

NSManagedObjectContext *context;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Game" inManagedObjectContext:context];
NSArray *games = [context executeFetchRequest:request error:nil];

NSMutableArray *filteredGames = [[NSMutableArray alloc] init];
for (Game *game in games) {
    NSInteger maxRoundNumber = [game.rounds valueForKeyPath:@"@max.number"];
    Round *maxRound = [[game.rounds filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"number = %d", maxRoundNumber]] anyObject];
    if (maxRound.someBoolProperty)
        [filteredGames addObject:game];
}

游戏有 的NSSet属性roundsRounds并且Round有 的NSInteger属性number,我正在寻找那些Games最高数Round有一些BOOL属性的人someBoolProperty

4

1 回答 1

2

这似乎做你想做的事:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Game"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SUBQUERY(rounds, $r, $r.someBoolProperty = YES).number = @max.rounds.number"];
于 2013-01-06T17:46:50.070 回答