首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >CoreData编辑/覆盖对象

CoreData编辑/覆盖对象
EN

Stack Overflow用户
提问于 2010-11-28 01:47:51
回答 2查看 18.3K关注 0票数 17

我正在玩一个新的项目,一个使用核心数据的拆分视图iPad应用程序,我想知道,因为它非常清楚如何添加和删除项目。如果我说将其更改为包含文本,那么文本将显示在UITextView中,如何在CoreData中编辑或覆盖对象

因此,用户在UITextView中键入他们的备注,当他们离开时,它将编辑并保存他们当前选择的备注(表视图中的对象)。

谢谢你的帮助谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-28 01:57:06

您只需使用NSFetchRequest请求现有对象,更改需要更新的任何字段(只需一个简单的myObject.propertyName设置器),然后在数据上下文上执行保存操作。

编辑以添加代码示例。我同意MCannon的观点,核心数据绝对值得一读。

此代码假设您使用包含核心数据内容的模板创建了项目,例如您的应用程序委托具有托管对象上下文等。请注意,这里没有错误检查,这只是基本代码。

获取对象的

代码语言:javascript
复制
// 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;

更新对象

代码语言:javascript
复制
thisYourEntityName.ExampleNSStringAttributeName = @"The new value";
thisYourEntityName.ExampleNSDateAttributeName = [NSDate date];

保存更改

代码语言:javascript
复制
NSError *error;
[self.managedObjectContext save:&error];

现在,您的对象/行已更新。

票数 35
EN

Stack Overflow用户

发布于 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将向您展示如何更改属性并保存它们。

核心数据是你真正想要阅读大量苹果文档并熟悉它的地方,从长远来看,它将为你节省几个小时。希望这能有所帮助!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4293026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档