我有2个实体之间的多对多关系。运动员(evals)LT; - >>的eval(whosEval)。我试图只显示被选中的运动员的遗嘱。但是,当我尝试通过eval.whosEval访问该关系时,我得到一个未定义的错误。当我运行应用程序时,表是空的,而如果我注释掉谓词,它会显示所有运动员的所有Evals。我错过了什么吗?谢谢。
allEvals.m
-(void)viewWillAppear:(BOOL)animated{
self.title = [NSString stringWithFormat:@"%@'s Evaluations",_athletesFullName];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSFetchRequest *athleteRequest = [[NSFetchRequest alloc] init];
[athleteRequest setEntity:[NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext]];
NSError *athleteError = nil;
NSArray *results = [_managedObjectContext executeFetchRequest:athleteRequest error:&athleteError];
NSPredicate *athletePredicate = [NSPredicate predicateWithFormat:@"full == %@", _athletesFullName];
[request setPredicate:athletePredicate];
Athlete *currentAthlete = [results objectAtIndex:0];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whosEval == %@", currentAthlete];
[request setPredicate:predicate];
NSEntityDescription *eval = [NSEntityDescription entityForName:@"Eval" inManagedObjectContext:_managedObjectContext];
[request setEntity:eval];
NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"date_recorded"
ascending:NO
selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil){
//handle error
}
[self setEvalArray:mutableFetchResults];
[self.tableView reloadData];
NSLog(@"Athlete's Full Name is: %@",_athletesFullName);
}
答案 0 :(得分:1)
你打电话
NSLog(@"This eval is for: %@", eval.whosEval);
但您将eval
声明为:
NSEntityDescription *eval = ...
所以类NSEntityDescription
无法知道“whosEval”是什么。
从Eval
检索mutableFetchResults
对象的实际实例并在其上调用whosEval
:
if (mutableFetchResults == nil){
//handle error
} else {
[self setEvalArray:mutableFetchResults];
[self.tableView reloadData];
NSLog(@"Athlete's Full Name is: %@",_athletesFullName);
NSLog(@"This eval is for: %@", [[mutableFetchResults lastObject] whosEval]);
}
答案 1 :(得分:1)
你的谓词在应该真正使用运动员对象时使用运动员名称:
@"whosEval == %@", self.athlete