如何更新核心数据中的现有对象? [迅速] [英] How to update existing object in core data ? [Swift]

查看:82
本文介绍了如何更新核心数据中的现有对象? [迅速]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将.csv文件中的数据预加载到coredata中。我以下列方式获取数据

  var places:[Places] = [] 

pre> if let managedObjectContext =(UIApplication.sharedApplication()。delegate as!AppDelegate).managedObjectContext {
let fetchRequest = NSFetchRequest(entityName:Places)
do {
places = try managedObjectContext.executeFetchRequest(fetchRequest)as! [Places]
}

catch让错误为NSError {
print(无法检索记录:\(error.localizedDescription))
}
}

在数据中有一个类型为String的属性isFavorite,其初始值为false。我改变了isFavorite的值在按钮点击。我想保存用户所做的更改。 如何使此更改持续?



以下是我的按钮操作

  @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 的值并保存到核心数据



EDIT :如果我尝试这里面我的按钮动作方法

 如果let managedObjectContext =(UIApplication.sharedApplication delegate as!AppDelegate).managedObjectContext {

let place:Places = Places()
place.isFavourite = cell.isFavouriteLabel.text
do {
try managedObjectContext。 save()
} catch let错误为NSError {
print(error)

}
}

我遇到错误:无法调用NSManagedObject类指定的初始化程序



如果我只是在按钮操作方法中添加此代码

  places.isFavourite = cell.isFavouriteLabel.text 



我收到此错误:[Places]没有名为isFavourite的成员

解决方案

您当前的代码是:

 如果让managedObjectContext =(UIApplication.sharedApplication()。delegate as! AppDelegate).managedObjectContext {

let place:Places = Places()
place.isFavourite = cell.isFavouriteLabel.text
do {
try managedObjectContext.save
} catch让错误为NSError {
print(error)

}
}

这将创建一个新地点(如果有效),但您需要更新现有地点。



managedObjectContext.executeFetchRequest 返回 places



所以你需要得到像 places [index_of_the_cell_in_question] .isFavourite = cell.isFavouriteLabel.text



然后 managedObjectContext.save()


I have preloaded data from a .csv file into coredata. I am fetching the data in the following way

var places:[Places] = []

in 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)")
            }
        }

In the data there is an attribute isFavorite of type String whose initial value is false. I am changing the value of isFavorite on button click. I want to save the changes made by the user. How can i make this change persistent ?

Here is my button action

@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"

        }
}

Basically i want to set the value of places.isFavourite = cell.isFavoriteLabel.text and save to core data

EDIT : if i try this inside my button action method

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)

            }
          }

i am getting an error : Failed to call designated initializer on NSManagedObject class

if i simply add this code in the button action method

places.isFavourite = cell.isFavouriteLabel.text

i get this error : [Places] does not have a member named isFavourite

解决方案

Your current code is:

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)

            }
          }

That would create a new place (if it worked), but you need to update an existing one.

You have the places returned from managedObjectContext.executeFetchRequest.

So you need to get something like places[index_of_the_cell_in_question].isFavourite = cell.isFavouriteLabel.text

and then managedObjectContext.save().

这篇关于如何更新核心数据中的现有对象? [迅速]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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