Swift - 从字典中删除一个数组

Posted

技术标签:

【中文标题】Swift - 从字典中删除一个数组【英文标题】:Swift - Remove an array from dictionary 【发布时间】:2019-01-17 22:08:40 【问题描述】:

我试图从字典内的数组中删除一个元素,但是当我从表视图中删除它时,该值并未从字典中删除。我尝试删除数组的方式是否错误?

marker.buttonAction["button actions array"]?.remove(at: indexPath.row)

//Function to remove array from dictionary and tableview.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) 

    if editingStyle == .delete 

        if let marker = markUpPlist.arrayObjects.filter($0.tagClip == currentSelectedMarker).first 

            print(marker.buttonAction["button actions array"]?[indexPath.row].action)

                marker.buttonAction["button actions array"]?.remove(at: indexPath.row)
                tableView.beginUpdates()
                tableView.deleteRows(at: [indexPath], with: .automatic)
                tableView.endUpdates()
        
    


//Variable which stores the dictionary
var buttonAction : [String: [ButtonAction]] = [:]

//ButtonAction array
class ButtonAction: Codable 
var action: String
var array_linked_of_buttons: [[String:String]]

init(action: String, array_linked_of_buttons: [[String:String]]) 
 self.action = action
 self.array_linked_of_buttons = array_linked_of_buttons
  

【问题讨论】:

【参考方案1】:

答案是我忘记在删除数据后重新保存数据。代码工作正常,如果这对其他人有帮助,我会留下问题。

【讨论】:

arrayObjects 属于类还是结构? arrayObjects 是一个类 如果你的数据源是markUpPlist.arrayObjects,那么一个类的副本很奇怪

以上是关于Swift - 从字典中删除一个数组的主要内容,如果未能解决你的问题,请参考以下文章

从字典数组 Swift 中填充 UITableView

从字典数组映射到 Swift 中的数字数组

如何在swift 3中从字典中获取数组

删除 SwiftyJSON 数组中的字典记录

从 Json 数据中分组,其中包含 Swift 4 中的字典数组

swift - 从嵌套字典中删除或替换 <null> 值