【问题标题】:How to remove specific key value from array of dictionary from User Defaults swift3如何从用户默认值swift3中的字典数组中删除特定键值
【发布时间】:2018-07-14 07:14:13
【问题描述】:

我有一个字典数组保存在用户默认值中。我在 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
    }

【问题讨论】:

  • 您正在向notificationArray 添加一个新字典,而不是删除所需的元素。
  • 我已经尝试过这个:- var myNotificationArray = UserDefaults.standard.object(forKey: "notificationArray") as? [AnyHashable] myNotificationArray?.remove(at: indexPath.row) UserDefaults.standard.set(myNotificationArray, forKey: "notificationArray") UserDefaults.standard.synchronize(),但它不起作用。
  • 您只需要notificationArray.remove(at:indexPath.row) 并将其保存回用户默认值。

标签: ios swift uitableview nsdictionary nsuserdefaults


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 2012-10-07
    • 2014-03-21
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    相关资源
    最近更新 更多