gpt4 book ai didi

ios - 如何删除 managedObject - 而不是 tableView

转载 作者:行者123 更新时间:2023-11-28 16:04:41 24 4
gpt4 key购买 nike

我在核心数据图中描述了一些颜色信息。实体是颜色,属性是颜色分量。

我在两个方面苦苦挣扎:如何从图表中删除颜色对象,其次,(奖金问题?),我如何识别重复的颜色?

在我的 AppDelegate 中,我有一个像这样的核心数据栈:

lazy var persistentContainer: NSPersistentContainer = {

let container = NSPersistentContainer(name: "DD")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replacing this implementation with code to handle the error appropriately.

fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

// MARK: - Core Data Saving support

func saveContext () {
print(#function)
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}

在我试图删除颜色的地方,我有这个:

func deleteColor(_ sender:UIButton) {

let i : Int = (sender.layer.value(forKey: "index")) as! Int
print(#function, "object to delete: ", i)

let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

colors.remove(at: i)

do {
try managedContext.save()
} catch let error as NSError {
print("Error While Saving Data: \(error.userInfo)")
}

recentColorCollection!.reloadData()
}

变量是:

var colors = [RecentColorEntity]()
var colorEntity = "RecentColorEntity"

我没有收到任何错误,但对象没有被删除。有人能帮我弄清楚我做错了什么吗

最佳答案

colors.remove(at: i)

只是从内存中的颜色数组中删除颜色。您需要像这样删除实际对象

context.delete(colorObject)

并保存。

关于ios - 如何删除 managedObject - 而不是 tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40471388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com