arrays - 从字典数组中删除值

标签 arrays swift dictionary

如何从字典数组中删除字典?

我有一个字典数组,如下所示:var posts = [[String:String]]() - 我想删除具有特定键的字典,我该如何进行?我知道我可以通过执行 Array.removeAtIndex(_:) 从标准数组中删除一个值,或者通过执行 key.removeValue() 删除字典键,但是一个数组字典更棘手。

提前致谢!

最佳答案

如果我理解正确你的问题,这应该有效

var posts: [[String:String]] = [
    ["a": "1", "b": "2"],
    ["x": "3", "y": "4"],
    ["a": "5", "y": "6"]
]

for (index, var post) in posts.enumerate() {
    post.removeValueForKey("a")
    posts[index] = post
}

/* 
This will posts = [
    ["b": "2"], 
    ["y": "4", "x": "3"], 
    ["y": "6"]
]
*/

因为你的字典和包装数组都是值类型,只要修改循环内的 post 就会修改字典的副本(我们通过使用 var 声明它来创建它>),所以总而言之,我们需要将 index 处的值设置为字典的新修改版本

关于arrays - 从字典数组中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38212554/

相关文章:

c++ - std::map 设计:为什么 map 接受比较器作为模板参数

c - 读取以零开头的 int 并将其存储到 C 中的数组中

ios - SpriteKit : better way to temporarily change velocity on sprite?

python - 从字符串或列表创建字典

ios - 我如何在 Swift 中制作像 "Android Navigation Drawer Menu"这样的重叠菜单

swift - 尝试为图表版本 3.0.01 安装图表 Cocoa Pods,但我得到的是 3.0.5,为什么?

python - 列表列表到字典到pandas DataFrame

python - 如何提取 m × m 矩阵中的每个 n × n 矩阵

javascript - Javascript中的空白标识符

c++ - 处理从 C 中的函数返回的 char 数组