objective-c – CoreData编辑覆盖对象

objective-c – CoreData编辑覆盖对象,第1张

概述我正在玩一个新的项目,一个使用Core Data的分割视图iPad应用程序,我想知道,因为它相当清楚如何添加和删除项目。如果我想改变这个来保存文本,那么该文本会显示在UITextView中,我如何编辑覆盖CoreData中的一个对象? 所以用户在UITextView中键入他们的笔记,当他们离开时,它会编辑并保存当前所选的笔记(表格视图中的对象)。 感谢任何帮助谢谢。 您只需使用NSFetchRe 我正在玩一个新的项目,一个使用Core Data的分割视图iPad应用程序,我想知道,因为它相当清楚如何添加和删除项目。如果我想改变这个来保存文本,那么该文本会显示在UITextVIEw中,我如何编辑或覆盖CoreData中的一个对象?

所以用户在UITextVIEw中键入他们的笔记,当他们离开时,它会编辑并保存当前所选的笔记(表格视图中的对象)。

感谢任何帮助谢谢。

解决方法 您只需使用NSFetchRequest请求现有对象,更改任何需要更新的字段(一个简单的myObject.propertyname setter是所有必需的),然后对数据上下文执行保存 *** 作。

编辑添加代码示例。我同意MCannon,Core Data绝对值得一读。

此代码假定您使用包含Core Data的模板创建了项目,以便您的应用程序委托具有托管对象上下文等。请注意,这里没有错误检查,这只是基本代码。

获取对象

// RetrIEve the contextif (managedobjectContext == nil) {    managedobjectContext = [(YourAppnameAppDelegate *)[[UIApplication sharedApplication] delegate] managedobjectContext];}// RetrIEve the entity from the local store -- much like a table in a databaseNSEntityDescription *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 databasenspredicate *predicate = [nspredicate predicateWithFormat:@"YourIDentifyingObjectProperty == %@",yourIDentifyingQualifIEr];[request setPredicate:predicate];// Set the sorting -- mandatory,even if you're fetching a single record/objectNSSortDescriptor *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];

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

总结

以上是内存溢出为你收集整理的objective-c – CoreData编辑/覆盖对象全部内容,希望文章能够帮你解决objective-c – CoreData编辑/覆盖对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1086574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存