更新一个属性核心数据中的所有值 [英] Update all value in one attribute Core Data

查看:27
本文介绍了更新一个属性核心数据中的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用数组从Core Data中的一个属性获取所有值.我只需要按下一个按钮,并将所有值都设为-1,然后将其保存回Core Data.

I know how to fetch all value from one attribute in Core Data using an array. I just need to press a button and -1 all the value and save it back to the Core Data.

如何迅速更新所有值?

谢谢.

推荐答案

尝试以下示例可能会有所帮助.替换您的实体名称.

Try following example it might be helpful.Replace you entity name.

  var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)

  var context: NSManagedObjectContext = appDel.managedObjectContext!
  var request = NSFetchRequest(entityName: "Params")
  var params = NSEntityDescription.insertNewObjectForEntityForName("Params", inManagedObjectContext: context) as! NSManagedObject

  if let fetchResults = appDel.managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [NSManagedObject] {

       if fetchResults.count != 0{
             for (var v = 0 ; v < fetchResults.count ; v++) {
              var managedObject = fetchResults[v]

             println(fetchResults)
             managedObject.setValue("-1", forKey: "value")
             context.save(nil)

                }
        }
  }

这篇关于更新一个属性核心数据中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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