0

我是 xcode 的新手。这是我的调试说:

2012-07-22 18:40:51.787 TestSaveData[2329:fb03] Saved for number one
2012-07-22 18:40:55.159 TestSaveData[2329:fb03] Successfully load for test nombre one
2012-07-22 18:40:55.160 TestSaveData[2329:fb03] Successfully load for test number 2
2012-07-22 18:40:55.161 TestSaveData[2329:fb03] Successfully load for test number two
2012-07-22 18:40:55.161 TestSaveData[2329:fb03] Successfully load for test one
2012-07-22 18:40:55.162 TestSaveData[2329:fb03] Successfully load for test again
2012-07-22 18:40:55.163 TestSaveData[2329:fb03] Successfully load for 
2012-07-22 18:40:55.164 TestSaveData[2329:fb03] Successfully load for test one
2012-07-22 18:40:55.165 TestSaveData[2329:fb03] Successfully load for test one again
2012-07-22 18:40:55.166 TestSaveData[2329:fb03] Successfully load for test one again
2012-07-22 18:40:55.166 TestSaveData[2329:fb03] Successfully load for 1
2012-07-22 18:40:55.167 TestSaveData[2329:fb03] Successfully load for (null)
2012-07-22 18:40:55.170 TestSaveData[2329:fb03] Successfully load for number one

第一行是我使用 UITextField 的输入,我写了“第一”并使用此代码使用保存按钮 1 保存它,并在我按下按钮 1 时显示它

2012-07-22 18:40:51.787 TestSaveData[2329:fb03] Saved for number one

- (IBAction)button1:(id)sender
{
    TestSaveDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    Highscore *record = [NSEntityDescription insertNewObjectForEntityForName:@"Highscore" inManagedObjectContext:context];
    record.highscore1 = textField.text;
    NSLog(@"Saved for %@", textField.text);
    NSError *error;
    if (![context save:&error])
    {
        NSLog(@"Error");
    }
}

当我单击 button2 时,我的调试视图会查看我所有的旧输入数据。但在我的 UILabel 中,使用下面的这个按钮 2 只显示我的最后一个输入是“第一”

- (IBAction)button2:(id)sender
{
    TestSaveDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Highscore" inManagedObjectContext:context];
    [request setEntity:entity];
    NSError *error;
    if (![context save:&error])
    {
        NSLog(@"Error");
    }
    NSArray *array = [context executeFetchRequest:request error:&error];
    for (Highscore *record in array)
    {
        label.text = record.highscore1;
        NSLog(@"Successfully load for %@", record.highscore1);
    }
}

我的问题是,我有没有可能只显示最后一个输入?在这种情况下,如果我保存“第一”并且调试只显示“第一”而不显示我的旧数据。

4

1 回答 1

0

您可以在您的Highscore实体中包含一个日期/时间字段,按该字段排序,然后将其设置fetchLimit为 1。您将保存分数的历史记录,但仅检索最新的。

于 2012-07-22T11:59:18.397 回答