如何清除所有旧的保存数据并仅显示核心数据xcode中的最后数据?

时间:2012-07-22 10:56:02

标签: iphone ios5 xcode4.3

我是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的输入,我写“第一”并使用保存button1使用此代码保存它并在我按下button1时显示它

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

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

1 个答案:

答案 0 :(得分:0)

您可以在Highscore实体中添加日期/时间字段,按此排序,并将fetchLimit设置为1.您将保存分数的历史记录但只检索最新的分数。< / p>