使用CoreData不会保存对象

时间:2016-02-01 22:11:54

标签: ios swift core-data

我正在尝试使用CoreData保存对象。所有代码都可以正确执行,但数据不会被保存。有谁知道这是什么问题?

Done

1 个答案:

答案 0 :(得分:1)

try record.managedObjectContext!.save()有些问题 试试这种方式可能对你有所帮助

 let appDelegate =
  UIApplication.sharedApplication().delegate as! AppDelegate

  let managedContext = appDelegate.managedObjectContext


  let entity =  NSEntityDescription.entityForName("Person",
    inManagedObjectContext:managedContext)

  let person = NSManagedObject(entity: entity!,
    insertIntoManagedObjectContext: managedContext)


  person.setValue(name, forKey: "name")


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