修改字典[String:[String]]

时间:2018-09-19 08:37:33

标签: ios swift dictionary

如何在这样的字典中插入和删除值?

var example: [String: [String]]

在commit editStyle中,我必须删除值:

    if editingStyle == .delete {

       if indexPath.row >= 1 {

        let key = tableViewData[indexPath.section].dog
        let value = tableViewData[indexPath.section].name[indexPath.row - 1]

        let sections = IndexSet.init(integer: indexPath.section)
        tableView.reloadSections(sections, with: .none)

        DataManager.shared.example.removeValue(forKey: key[value])

        }

  }

谢谢!

1 个答案:

答案 0 :(得分:1)

如果我对您的理解正确,那么您想添加和删除属于字典中给定键的数组

要添加:

example[key]?.append(value)

删除:

if let index = example[key]?.firstIndex(of: value) {
    example[key]?.remove(at: index)
}
相关问题