将对象保存在CoreData中 [英] Save object in CoreData

查看:90
本文介绍了将对象保存在CoreData中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CoreData与iPhone SDK。我在做笔记应用程序。我有一个表,从我的模型显示注释对象。当按下按钮时,我想将文本视图中的文本保存到正在编辑的对象。我如何做到这一点?

I am using CoreData with iPhone SDK. I am making a notes app. I have a table with note objects displayed from my model. When a button is pressed I want to save the text in the textview to the object being edited. How do I do this? I've been trying several things but none seem to work.

感谢

编辑:

NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newManagedObject setValue:detailViewController.textView.text forKey:@"noteText"];

NSError *error;
if (![context save:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

上述代码正确保存,但将其另存为新对象。我想把它保存为我在tableView中选择的那个。

The above code saves it correctly but it saves it as a new object. I want it to be saved as the one I have selected in my tableView.

推荐答案

https://developer.apple.com/library/watchos/documentation/Cocoa/Conceptual/CoreData/index.html =nofollow noreferrer>核心数据编程指南。很难准确地知道你想从这个问题,但基本的想法是:

You should check out the Core Data Programming Guide. It's hard to know exactly what you want from the question, but the basic idea is:

-(IBAction)saveNote { //hooked up in Interface Builder (or programmatically)
    self.currentNote.text = self.textField.text; //assuming currentNote is an NSManagedObject subclass with a property called text, and textField is the UITextField
}

//later, at a convenient time such as application quit
NSError *error = nil;
[self.managedObjectContext save:&error];  //saves the context to disk

编辑:如果要编辑预先存在的对象,来自所获取的结果控制器的对象,例如 NSManagedObject * currentObject = [fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]] ,然后编辑该对象。我还建议使用NSManagedObject的自定义子类与属性声明,而不是使用 setValue:forKey ,因为它更灵活。

If you want to edit a preexisting object, you should get the object from the fetched results controller, e.g. NSManagedObject *currentObject = [fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]], then edit that object. I'd also recommend using a custom subclass of NSManagedObject with property declarations, rather than using setValue:forKey, since it's more flexible.

这篇关于将对象保存在CoreData中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆