如何在swift中更新指定的记录

时间:2015-10-14 21:27:20

标签: swift

我按代码保存数据: -

let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!
    let ent = NSEntityDescription.entityForName("Login", inManagedObjectContext: context)

    var newUser = Login(entity: ent!, insertIntoManagedObjectContext: context)

    newUser.cardnamber = txtCardNamber.text
    newUser.mobilenamber = txtMobileNamber.text
    newUser.generationcode = GC
    newUser.status = "inactive"



    context.save(nil)

    println(newUser)

请我想要更新指定的记录......

1 个答案:

答案 0 :(得分:0)

首先你需要获取登录。您还可以使用predicate过滤fetch result

然后,修改它并save context

就像这个例子:

  let fetchRequest = NSFetchRequest(entityName: "Login")
  do {
    let results =
      try managedContext.executeFetchRequest(fetchRequest) // list of Login in array
    people = results as! [NSManagedObject]
  } catch let error as NSError {
    print("Could not fetch \(error), \(error.userInfo)")
  }

let firstLogin = results[0] as! Login // get the Login you wants
firstLogin.status = "active" // modify it

context.save() // save it

享受! ;)