0

以下のコードでコア データのオブジェクトを更新しようとしましたが、エラーが発生しました。

    NSError *error;
    NSArray *objects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    NSLog(@"objects %@",objects);


    // yourIdentifyingQualifier is unique. It just grabs the first object in the array.
    AllChallenge *tempChallenge = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] objectAtIndex:0];


    tempChallenge =[[AllChallenge alloc] init];
    NSLog(@"tempchallenge >>>>> %@",tempChallenge);

    // update the object

    tempChallenge.status = 1;



    [self.managedObjectContext save:&error];

コンパイル後、CoreData: エラー: NSManagedObject クラス 'AllChallenge' で指定された初期化子の呼び出しに失敗しました。助けてくれてありがとう。

4

1 に答える 1

1

既存のオブジェクトを更新したい場合は、新しく割り当てられたオブジェクトに置き換えるべきではありません:

AllChallenge *tempChallenge = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] objectAtIndex:0];
// tempChallenge =[[AllChallenge alloc] init]; // <-- REMOVE THIS LINE
tempChallenge.status = 1;
[self.managedObjectContext save:&error];

新しいオブジェクトを作成したい場合は、指定された初期化子、またはこの便利なメソッドを使用する必要があります:

AllChallenge *tempChallenge = [NSEntityDescription insertNewObjectForEntityForName:@"AllChallenge" inManagedObjectContext: managedObjectContext];
于 2013-08-30T12:54:37.770 に答える