我是 NSPredicate 的新手,所以如果有人能告诉我我是否走在正确的道路上,我将不胜感激。
问题:(这些类是 NSManagedObject 的子类)
我有一个带有此字段的Player 类:
@property (nonatomic, retain) NSSet *hisNumbers; //it's a set of Numbers
Numbers 类是这样的:
@property (nonatomic, retain) NSNumber * first;
@property (nonatomic, retain) NSNumber * second;
@property (nonatomic, retain) Season *forSeason; //it's one-to-one relationship with a Season Object
@property (nonatomic, retain) Player *owner; //one-to-one with Player
@property (nonatomic, retain) NSNumber * isPresent;
Season Class具有以下参数:
@property (nonatomic, retain) NSSet *seasonGames; //set of Game
@property (nonatomic, retain) NSSet *numbers; //set of Numbers (inverse of forSeason)
我正在尝试做的是一种方法,它需要一个赛季对象并将那个赛季的所有球员还给我......
这是我的尝试:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"nameLast" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY SELF.hisNumbers.isPresent == %@) AND (ANY SELF.hisNumbers IN %@)",[NSNumber numberWithBool:YES],season.numbers];
但它不起作用..查询的两个部分似乎分开工作,但也许这只是一个巧合......
现在它返回每个玩家的号码带有标志'isPresent == YES'和另一个号码是'season.numbers'的成员。相反,我需要的是拥有一个同时符合两个标准的“数字”的球员......
如果您需要更多详细信息,请告诉我.. 谢谢大家