gpt4 book ai didi

ios - 删除 CoreData 对象

转载 作者:行者123 更新时间:2023-11-28 12:07:38 25 4
gpt4 key购买 nike

我还在为 CoreData 苦苦挣扎,开始新的一周哈哈。我终于成功地保存和获取了我的数组,现在是编辑和删除的时候了。

我先添加了删除函数,但我在传递正确的参数时遇到了问题:

核心数据函数:

class CDHandler: NSObject {

private class func getContext() -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}

class func saveObject(name:String, code:String, symbol:String, placeholder:String, amount:String) -> Bool {
let context = getContext()
let entity = NSEntityDescription.entity(forEntityName: "CryptosMO", in: context)
let managedObject = NSManagedObject(entity: entity!, insertInto: context)

managedObject.setValue(name, forKey: "name")
managedObject.setValue(code, forKey: "code")
managedObject.setValue(symbol, forKey: "symbol")
managedObject.setValue(placeholder, forKey: "placeholder")
managedObject.setValue(amount, forKey: "amount")

do {
try context.save()
return true
} catch {
return false
}
}

class func fetchObject() -> [CryptosMO]? {
let context = getContext()
var cryptos: [CryptosMO]? = nil

do {
cryptos = try context.fetch(CryptosMO.fetchRequest()) as? [CryptosMO]
return cryptos
} catch {
return cryptos
}
}

class func deleteObject(crypto: CryptosMO) -> Bool {
let context = getContext()
context.delete(crypto)

do {
try context.save()
return true
} catch {
return false
}
}

创建并保存数组:

    if addedCrypto != "" {
if addedCrypto == "Bitcoin BTC" {
if CDHandler.saveObject(name: "Bitcoin", code: "bitcoin", symbol: "BTC", placeholder: "BTC Amount", amount: "0.0") {
for crypto in CDHandler.fetchObject()! {
print("\(String(describing: crypto.name))'s symbol is \(String(describing: crypto.symbol))")
}
}
}
}

为 TableView 获取核心数据:

override func viewWillAppear(_ animated: Bool) {
tableView.delegate = self
tableView.dataSource = self


if CDHandler.fetchObject() != nil {
cryptos = CDHandler.fetchObject()!
tableView.reloadData()
}
}

表格 View 函数:

extension WalletTableViewController: UITableViewDelegate, UITableViewDataSource, CryptoCellDelegate {


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cryptos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WalletTableViewCell

cell.cryptoNameLabel.text = cryptos[indexPath.row].name
cell.amountTextField.placeholder = cryptos[indexPath.row].placeholder

cell.delegate = self
cell.amountTextField.delegate = self

return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
cryptos.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
CDHandler.deleteObject(crypto: cryptos) // <----- Cannot convert value of type '[CryptosMO]' to expected argument type 'CryptosMO'
}
}

这里有什么问题?我可以将 func deleteObject(crypto: CryptosMO) 更改为 func deleteObject(crypto: [CryptosMO]) 但是我得到 Cannot convert value of type '[CryptosMO] ' 到预期的参数类型 'NSManagedObject'

我读到 delete() 仅将 NSManagedObject 作为其唯一参数,所以我相信我首先创建了一个不正确的对象以便能够删除它? ?

最佳答案

只需调用此方法并使用要删除的 managedObject 传递实体:

func deleteData(entity:String,deleteObject:NSManagedObject){
//for iOS 10+
// let delegate = UIApplication.shared.delegate as? AppDelegate
// let context = delegate!.persistentContainer.viewContext
let context = getContext()
context.delete(deleteObject)
do {
try context.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}

}


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let selectedManagedObject = cryptos[indexPath.row]
deleteData(entity:"yourEntityName",deleteObject: selectedManagedObject)
cryptos.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}

和save方法一样可以编辑,只需要传递你要编辑的managedObject即可:

class func updateObject(name:String, code:String, symbol:String, placeholder:String, amount:String,selectedManagedObject:NSManagedObject) {
let context = getContext()

selectedManagedObject.setValue(name, forKey: "name")
selectedManagedObject.setValue(code, forKey: "code")
selectedManagedObject.setValue(symbol, forKey: "symbol")
selectedManagedObject.setValue(placeholder, forKey: "placeholder")
selectedManagedObject.setValue(amount, forKey: "amount")

do {
try context.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}

}

关于ios - 删除 CoreData 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49110043/

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