我正在玩一个新的项目,一个使用核心数据的拆分视图iPad应用程序,我想知道,因为它非常清楚如何添加和删除项目。如果我说将其更改为包含文本,那么文本将显示在UITextView
中,如何在CoreData中编辑或覆盖对象
因此,用户在UITextView
中键入他们的备注,当他们离开时,它将编辑并保存他们当前选择的备注(表视图中的对象)。
谢谢你的帮助谢谢。
发布于 2010-11-28 01:57:06
您只需使用NSFetchRequest
请求现有对象,更改需要更新的任何字段(只需一个简单的myObject.propertyName设置器),然后在数据上下文上执行保存操作。
编辑以添加代码示例。我同意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];
现在,您的对象/行已更新。
发布于 2010-11-28 09:04:25
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将向您展示如何更改属性并保存它们。
核心数据是你真正想要阅读大量苹果文档并熟悉它的地方,从长远来看,它将为你节省几个小时。希望这能有所帮助!
https://stackoverflow.com/questions/4293026
复制相似问题