gpt4 book ai didi

ios - 如何从用户默认值 swift3 的字典数组中删除特定键值

转载 作者:行者123 更新时间:2023-11-30 11:48:07 24 4
gpt4 key购买 nike

我在用户默认值中保存了一组字典。我在 UITableview 中显示这些值。当用户向右滑动表格单元格并将其删除时,该单元格已成功删除,但实际上并未从用户默认值中删除。这是我尝试过的:

   var notificationArray: [[String: AnyObject]] = []
var title = [String]()
var detail = [String]()

override func viewDidLoad() {
super.viewDidLoad()
if let tempArray = UserDefaults().array(forKey: "notificationArray") as? [[String: AnyObject]] {

notificationArray = tempArray

self.title = tempArray.flatMap { $0["title"] as? String }
self.detail = tempArray.flatMap { $0["detail"] as? String }
}
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")

self.title.remove(at: indexPath.row)
self.detail.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)

notificationArray.append(["title": title as AnyObject, "detail": detail as AnyObject])
UserDefaults.standard.set(notificationArray, forKey: "notificationArray")
print("title, detail", title, detail)
}
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return title.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 80
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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


cell.notificationTittleLabel.text = title[indexPath.row]
cell.notificationDetailLabel.text = detail[indexPath.row]

cell.backgroundColor = UIColor.clear

return cell
}

最佳答案

这对我有用。谢谢,@Paulw11:)

  var myNotificationArray = UserDefaults.standard.object(forKey: "notificationArray") as? [AnyHashable]
myNotificationArray?.remove(at: indexPath.row)
UserDefaults.standard.set(myNotificationArray, forKey: "notificationArray")
UserDefaults.standard.synchronize()

关于ios - 如何从用户默认值 swift3 的字典数组中删除特定键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48605499/

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