【问题标题】:Access value of an entity attribute实体属性的访问值
【发布时间】:2013-08-18 20:15:12
【问题描述】:

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

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

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

allathletes.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;
}

【问题讨论】:

    标签: ios objective-c cocoa-touch cocoa core-data


    【解决方案1】:

    如果date_recordedNSDate 属性,那么您可以这样做

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

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

    【讨论】:

    • @user2674329:字符串的格式如何?要找到最后日期,必须以某种方式对它们进行比较/排序。 (使用 NSDate 对象会更容易。)
    • 它有一个日期的值,但是是一个字符串。像这样:“2013 年 8 月 18 日”。它已正确排序。
    • @user2674329:相关对象没有排序(除非你使用了ordered关系,它还有其他问题)。我只能建议将日期存储为日期,而不是字符串,并将它们转换为字符串仅用于显示。这使大多数事情变得容易得多。
    • 我就是这么做的。抱歉,如果我以令人困惑的方式解释了这一点。
    猜你喜欢
    • 2017-05-29
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    相关资源
    最近更新 更多