我在2个实体之间有一对多关系。运动员(evals)> Eval(whosEval)。我试图仅显示被选中运动员的评估。但是,当我尝试通过eval.whosEval访问关系时,出现未定义的错误。当我运行该应用程序时,该表为空,而如果我注释掉该谓词,它将显示所有运动员的所有评估。我想念什么吗?谢谢。

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);

}

最佳答案

当谓词应真正使用运动员对象时,便使用运动员名称:

@"whosEval == %@", self.athlete

09-07 11:17