Swift-从字典中删除数组

时间:2019-01-17 22:08:40

标签: arrays swift uitableview dictionary

我正在尝试从字典中的数组中删除元素,但是当我从tableview中删除它时,值并未从字典中删除。我尝试删除阵列的方法是否错误?

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 个答案:

答案 0 :(得分:1)

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