如何更新核心数据中的现有对象?
我已经从一个.csv文件预加载数据到coredata。 我以下面的方式获取数据
var places:[Places] = []
在viewDidLoad
:
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext { let fetchRequest = NSFetchRequest(entityName: "Places") do{ places = try managedObjectContext.executeFetchRequest(fetchRequest) as! [Places] } catch let error as NSError{ print("Failed to retrieve record: \(error.localizedDescription)") } }
在数据中有一个属性isFavorite,它的初始值为false。 我改变button点击isFavorite的价值。 我想保存用户所做的更改。 我怎样才能使这个改变持久?
这是我的button动作
@IBAction func addToFavourites(sender: AnyObject) { cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! CustomTableViewCell if cell.isFavouriteLabel.text! == "false" { cell.isFavouriteLabel.text = "true" }else if cell.isFavouriteLabel.text == "true"{ cell.isFavouriteLabel.text = "false" } }
基本上我想设置places.isFavourite = cell.isFavoriteLabel.text
的值并保存到核心数据
编辑 :如果我尝试这里面我的button操作方法
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext { let place : Places = Places() place.isFavourite = cell.isFavouriteLabel.text do{ try managedObjectContext.save() } catch let error as NSError{ print(error) } }
我得到一个错误 :无法调用NSManagedObject类上的指定初始值设定项
如果我只是在button操作方法中添加此代码
places.isFavourite = cell.isFavouriteLabel.text
我得到这个错误 :[Places]没有一个名为isFavourite的成员
您目前的代码是:
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext { let place : Places = Places() place.isFavourite = cell.isFavouriteLabel.text do{ try managedObjectContext.save() } catch let error as NSError{ print(error) } }
这将创build一个新的地方(如果它的工作),但你需要更新一个现有的。
你有从managedObjectContext.executeFetchRequest
返回的places
。
所以你需要得到像places[index_of_the_cell_in_question].isFavourite = cell.isFavouriteLabel.text
然后managedObjectContext.save()
。
使用NSManagedObjectContext的save
function:
places.isFavourite = cell.isFavoriteLabel.text var error: NSError? if managedObjectContext.save(&error) != true { // Error }
这很简单:
-
find你想要修改的
places
然后保存核心数据上下文。func saveContext () { if let moc = self.managedObjectContext { var error: NSError? = nil if moc.hasChanges && !moc.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. println("Unresolved error \(error), \(error!.userInfo)") abort() } } }
我build议你使用一个pipe理器在核心数据中插入,提取和删除条目。
import Foundation import CoreData class CoreDataHelper: NSObject { class var shareInstance:CoreDataHelper { struct Static { static let instance:CoreDataHelper = CoreDataHelper() } return Static.instance } //MARK: - Insert - func insertEntityForName(entityName:String) -> AnyObject { return NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: self.managedObjectContext!) } //MARK: - Fetch - func fetchEntitiesForName(entityName:String) -> NSArray { ... } //MARK: - Delete - func deleteObject(object:NSManagedObject) { self.managedObjectContext!.deleteObject(object) } // MARK: - Core Data Saving support - func saveContext () { if let moc = self.managedObjectContext { var error: NSError? = nil if moc.hasChanges && !moc.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. println("Unresolved error \(error), \(error!.userInfo)") abort() } } }
跳这可以帮助你