2015-08-09 120 views
0

所有值我知道如何從一個屬性中的核心數據使用數組提取所有的價值。我只需要按下一個按鈕並將所有的值保存到核心數據。更新一個屬性核心數據

我怎樣才能在迅速更新一次所有的價值呢?

謝謝。

回答

0

嘗試下面的例子可能是helpful.Replace您實體名稱。

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) 

       } 
     } 
    } 
0

爲SWIFT 3

let appDelegate = UIApplication.shared.delegate as! AppDelegate 
let managedContext = appDelegate.managedObjectContext 
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Params") 

    do{ 
     if let fetchResults = try managedContext.fetch(request) as? [NSManagedObject] { 
      if fetchResults.count != 0 { 
       for index in 0...fetchResults.count-1 { 
        let managedObject = fetchResults[index] 

        managedObject.setValue("-1", forKey: "value") 
       } 

       try managedContext.save() 
      } 
     } 
    } catch let error as NSError { 
     print("Could not fetch \(error), \(error.userInfo)") 
    }