gpt4 book ai didi

ios - 实体属性的访问值

转载 作者:行者123 更新时间:2023-11-29 13:07:13 26 4
gpt4 key购买 nike

我有一个 Core Data 对多关系,如下所示:

Athlete(evals)<->>Eval(whosEval)

我有一个运动员 TableView ,它显示数据库中的所有运动员。我想将字幕文本设置为 Eval 属性,假设它被称为“date_recorded”问题是,每个运动员可能有超过 1 个 eval。我需要选择 evalArray 中的最后一个对象,然后显示每个运动员的相关 Eval 属性,因此每个运动员的字幕文本可能会有所不同。我将如何为表格中的每个单元格执行此操作?这是我到目前为止所得到的:

所有运动员.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Athlete Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Athlete *athlete = (Athlete *)[athleteArray objectAtIndex:indexPath.row];
cell.textLabel.text =[athlete full];
cell.detailTextLabel.text = //eval attribute "date_recorded"

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;
NSPredicate *athletePredicate = [NSPredicate predicateWithFormat:@"full == %@", athlete.full];
[athleteRequest setPredicate:athletePredicate];
NSArray *results = [_managedObjectContext executeFetchRequest:athleteRequest error:&athleteError];
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
}

NSMutableArray *lastEvalArray = mutableFetchResults;

Eval *lastEval = lastEvalArray.lastObject;

cell.detailTextLabel.text = lastEval.date_recorded;

return cell;
}

最佳答案

如果 date_recorded 是一个 NSDate 属性,那么你可以这样做

Athlete *athlete = (Athlete *)[athleteArray objectAtIndex:indexPath.row];
NSDate *lastRecorded = [athlete valueForKeyPath:@"@max.evals.date_recorded"];

然后使用 NSDateFormatterlastRecorded 转换为 NSString 和将其分配给 cell.detailTextLabel.text

关于ios - 实体属性的访问值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18303228/

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