gpt4 book ai didi

objective-c - CoreData 获取请求 - 复杂谓词

转载 作者:行者123 更新时间:2023-11-29 11:04:45 24 4
gpt4 key购买 nike

我如何使用(理想情况下)带有键值编码集合运算符的单个谓词来简化这个过程? (最后感兴趣的数组是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 属性 rounds RoundsRound 有一个 NSInteger 属性 number,我正在寻找那些 Games,其最高编号 Round 具有一些 BOOL 属性someBoolProperty.

最佳答案

这似乎可以满足您的要求:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Game"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SUBQUERY(rounds, $r, $r.someBoolProperty = YES).number = @max.rounds.number"];

关于objective-c - CoreData 获取请求 - 复杂谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14184486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com