Ios 检查数据是否保存在CoreData上

Ios 检查数据是否保存在CoreData上,ios,swift,core-data,Ios,Swift,Core Data,我有一个名为“word”的字符串,我将该字符串存储到一个名为Lose的实体中,该实体位于属性word中, 我想当用户再次保存一个字符串“word”时,它会覆盖当前值而不是重复的值,如何做到这一点 到目前为止我所做的: //save the data to core data let library = Lose(context: savecatch.context) library.word = "word" savecatch.s

我有一个名为“word”的字符串,我将该字符串存储到一个名为Lose的实体中,该实体位于属性word中, 我想当用户再次保存一个字符串“word”时,它会覆盖当前值而不是重复的值,如何做到这一点

到目前为止我所做的:

        //save the data to core data
        let library = Lose(context: savecatch.context)
        library.word = "word"
        savecatch.saveContext()

在保存之前,我需要检查“word”是否已经存在

您必须获取具有给定属性的记录,然后修改并保存它

let query = "word"
let request = NSFetchRequest<Lose> = Lose.fetchRequest()
request.predicate = NSPredicate(format: "word == %@", query)
do {
   let result = try context.fetch(request)
   if let found = result.first {
       found.word = "word"
       context.save()
   }
} catch {
    print(error)
}
let query=“word”
let request=NSFetchRequest=Lose.fetchRequest()
request.predicate=NSPredicate(格式:“word==%@”,查询)
做{
let result=try context.fetch(请求)
如果let found=result.first{
found.word=“word”
context.save()
}
}抓住{
打印(错误)
}