私は午後中ずっと、アプリのCoreData部分を機能させ、ほぼ正常に機能させるように努めてきました。問題は、アプリが終了したとき(マルチタスクメニューから削除されたとき)にCoreDataが適切な値を保存しないことです。残っている値は、ユーザーが入力した新しい値の代わりに、エンティティに入力した初期値(私の場合は0、NSStringとして定義)です。そのため、問題は、アプリを閉じた後に新しい値を永続的に保存し、アプリが読み込まれたときに初期値が再び表示されないことです。
エンティティ名:Gameinfo
属性:score
何が間違っているのかわかりません。誰かが私を助けることができれば、それはありがたいです。ありがとう!
-(IBAction)saveTheory1
{
AppDelegate *appDelegate= [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Gameinfo"];
NSError *error = nil;
NSArray *someArray = [context executeFetchRequest:request error:&error];
[[someArray objectAtIndex:0] setValue:[self showKey] forKey:@"score"]; // showKey gets the text from UITextField
}
-(IBAction)showTheory1;
{
AppDelegate *appDelegate= [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Gameinfo"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request
error:&error];
matches = [objects objectAtIndex:0];
NSLog (@"show us the current value: %@", [matches valueForKey:@"score"]);
}