2010-11-27 17 views
16

私は新しいプロジェクト、コアデータを使用している分割ビューのiPadアプリで遊んでいます。アイテムの追加と削除の方法はかなり明確です。テキストを保持するためにこれを変更すると、そのテキストはUITextViewに表示されます。CoreDataでオブジェクトを編集または上書きするにはどうすればよいですか?CoreData編集/上書きオブジェクト

したがって、ユーザーはUITextViewにメモを入力します。ユーザーがメモを入力すると、現在選択しているメモ(テーブルビューのオブジェクト)が編集されて保存されます。

ありがとうございました。

答えて

33

は、あなたは、単にフィールドを更新する必要があるものは何でもNSFetchRequest、変更を(簡単なmyObject.propertyNameセッターはすべてのことが必要なのである)を使用して、既存のオブジェクトを要求し、その後データコンテキスト上の保存アクションを実行します。

EDITにコード例を追加してください。私はMCannonに同意します。

このコードでは、アプリケーションデリゲートに管理オブジェクトコンテキストなどがあるように、コアデータを含むテンプレートを使用してプロジェクトを作成したことを前提としています。ここでエラーをチェックするのは基本的なコードです。

オブジェクト

// Retrieve the context if (managedObjectContext == nil) { managedObjectContext = [(YourAppNameAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; } // Retrieve the entity from the local store -- much like a table in a database NSEntityDescription *entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:managedObjectContext]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entity]; // Set the predicate -- much like a WHERE statement in a SQL database NSPredicate *predicate = [NSPredicate predicateWithFormat:@"YourIdentifyingObjectProperty == %@", yourIdentifyingQualifier]; [request setPredicate:predicate]; // Set the sorting -- mandatory, even if you're fetching a single record/object NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"yourIdentifyingQualifier" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [request setSortDescriptors:sortDescriptors]; [sortDescriptors release]; sortDescriptors = nil; [sortDescriptor release]; sortDescriptor = nil; // Request the data -- NOTE, this assumes only one match, that // yourIdentifyingQualifier is unique. It just grabs the first object in the array. YourEntityName *thisYourEntityName = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0]; [request release]; request = nil; 

更新

をフェッチオブジェクト

thisYourEntityName.ExampleNSStringAttributeName = @"The new value"; 
thisYourEntityName.ExampleNSDateAttributeName = [NSDate date]; 

変更を保存し

NSError *error; 
[self.managedObjectContext save:&error]; 

これで、オブジェクト/行が更新されました。

+0

よろしくお願いします。コードサンプルをお願いしますか?申し訳ありませんが、私はCoreDataにかなり新しいが、サンプルを持っていることは本当に私を理解するのに役立つでしょう。 –

+1

私はいくつかの基本的なコードを含むように私の答えを編集しました。 –

+0

私は確かに読んで、助けてくれてありがとう。しかし、いくつかのことがあります:フェッチ要求は、それがpre - made( 'NSFetchedResultsController *)fetchedResultsController'を置き換えますか?また、 'YourEntityName * thisYourEntityName'は私を混乱させています、実際のエンティティ名ですか?ただエラーが出ます。そして、チャンクを更新して保存すると、フェッチコードがそのまま残っていますか? –

4

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOs.htmlプロパティを変更する方法をお見せし、それらを保存します、エンティティを取得する方法を紹介します。

コアデータは、Appleの多くのドキュメントをよく読んで親しみを持たせたいものです。長期間に渡って時間を節約できます。お役に立てれば!

+0

ページが見つかりませんでした。 – UnderDog

関連する問題