我有一个由id和detailText组成的Core数据实体。我的想法是,我的文本字段存储了它的内容以及某些id,这是我案例中的时间戳。现在,当用户第一次存储detailText时,它可以正常工作,但如果用户为特定id编辑detailText,我想删除旧的detailText并放入新的detailText。 基本上我需要帮助在Swift / Core数据中编写下面的查询。
UPDATE myEntity SET detailText = theNewDetailText WHERE ID = myID;
这些是我当前的保存和替换功能,我使用谓词来查找选择我的ID,但是我无法更新特定ID的相应detailText字段。
func saveTextFromTextFieldToCoreData(textToSave: String){
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("GoalDetail", inManagedObjectContext:managedContext)
let thingToSaveToCD = NSManagedObject(entity: entityDescription!, insertIntoManagedObjectContext: managedContext)
print("received text to store and it's \(textToSave)")
if entityIsEmpty("GoalDetail") == true {
thingToSaveToCD.setValue(data.timeStamp, forKey: "id")
thingToSaveToCD.setValue(textToSave, forKey: "detailText")
print("succesfully saved \(data.timeStamp) and \(textToSave)")
} else {
replaceTheValueFromCoreData()
}
}
func replaceTheValueFromCoreData(){
print("i have entered the delete core data item")
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "GoalDetail")
let predicate = NSPredicate(format: "id == %@", data.timeStamp)
fetchRequest.predicate = predicate
//let entityDescription = NSEntityDescription.entityForName("GoalDetail", inManagedObjectContext:managedContext)
//let thingToSaveToCD = NSManagedObject(entity: entityDescription!, insertIntoManagedObjectContext: managedContext)
do{
let goal = try appDelegate.managedObjectContext.executeFetchRequest(fetchRequest)
try managedContext.save()
}
catch{
//error
}
我已经通过谷歌和堆栈溢出,但到目前为止还没有设法执行此任务,所以任何帮助都非常感谢。
答案 0 :(得分:1)
我已经解决了它
AppCompatTextView