2016-08-03 87 views
8

我想在swift 3中更新核心数据对象。经过一些Google搜索后,我没有发现任何有关swift的问题3.因此,我的问题是:如何在swift中更新核心数据对象3?更新核心数据对象swift 3

+0

在你认为什么样的方式更新管理对象斯威夫特3是不同的?您遇到Swift 2中没有发生的特定问题吗?如果是这样,请分享更多细节。 – mic

回答

14

试试这个

 let empId = "001" 
     let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "EmpDetails") 
     let predicate = NSPredicate(format: "empId = '\(empId)'") 
     fetchRequest.predicate = predicate 
     do 
     { 
      let test = try context?.fetch(fetchRequest) 
      if test?.count == 1 
         { 
          let objectUpdate = test![0] as! NSManagedObject 
          objectUpdate.setValue("newName", forKey: "name") 
          objectUpdate.setValue("newDepartment", forKey: "department") 
          objectUpdate.setValue("001", forKey: "empID") 
          do{ 
           try context?.save() 
          } 
          catch 
          { 
           print(error) 
          } 
         } 
     } 
     catch 
     { 
      print(error) 
     } 
+2

工作就像一个魅力! – OHS

+1

谢谢!它运作良好。 – Raja

+0

谢谢,但是你必须让empID独一无二吗? – NavodaP