0

我在 2 个实体之间有一对多的关系。运动员(评估)<-->>评估(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);

}
4

2 回答 2

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]);
}
于 2013-08-17T21:28:08.917 回答
1

您的谓词在应该真正使用运动员对象时使用运动员名称:

@"whosEval == %@", self.athlete
于 2013-08-17T21:32:14.613 回答