从Core Data Swift 4中删除

时间:2018-04-07 13:53:19

标签: swift core-data

我有一个收藏夹按钮,单击该按钮会将特定字符的图像添加到CoreData。

var data = document.getElementById('data');
var canv = document.getElementById('canv');
canv.width = window.innerWidth - data.offsetWidth;
canv.height = window.innerHeight;

function c() {
  var text = document.getElementById('text');
  text.innerHTML = 'longname1/longname2/longname3/longname4';
}

我的问题是如何在再次点击按钮时从CoreData中删除该图像?

1 个答案:

答案 0 :(得分:3)

在coredata中,如果你想删除它,每个对象应该有Id,就像这个

let fetchRequest: NSFetchRequest<Favorite> = Favorite.fetchRequest()
fetchRequest.predicate = Predicate.init(format: "FavoriteID==\(ID)")

do {
    let objects = try context.fetch(fetchRequest)
    for object in objects {
        context.delete(object)
    }
    try context.save()
} catch _ {
    // error handling
}