在Swift中轻松访问和修改字典数组 [英] Access and modify easily Array of Dictionaries in Swift

查看:298
本文介绍了在Swift中轻松访问和修改字典数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的结构:[String: [String: Double]]()

具体来说,是这样的:var dictionaries = ["GF": ["ET": 4.62, "EO": 21.0],"FD": ["EE": 80.95, "DE": 0.4]]

如何轻松访问和修改嵌套词典?

示例更新:我想在FD处附加"TT": 6,以后再在数组内附加另一个字典.最后,我将打印结果.

for (key,value) in dictionaries {

    // if array contains FD, add the record to FD
    if key.contains("FD") {
        dictionaries["FD"]!["TT"] = 6
    }
    else {
    // if array doesn't contain FD, add FD and add the record to it
        dictionaries = dictionaries+["FD"]["TT"] = 6 // <-- I know that it's wrong but I want to achieve this result in this case.
    }
}

打印结果将是:

GF -> ET - 4.62, EO - 21.0
FD -> EE - 80.95, DE - 0.4, TT - 6

任务:我需要像上面的示例一样添加新的词典记录,以一种简单明了的方式更新现有的记录,轻松地遍历记录以读取值并打印出来.

有人可以帮助我吗?从现在开始,从来没有机会在Swift中管理字典.

解决方案

如果您可以使用key"FD",它将更加简单

dictionaries["FD"]!["TT"] = 6

添加新值["TT":6]或将TT的现有值修改为6.请注意!在["FD"]之间!["TT"]假定["FD"]存在.否则,您需要检查["FD"]是否存在.喜欢:

if dictionaries["FD"] != nil {
    dictionaries["FD"]!["TT"] = 6
} else {
    dictionaries["FD"] = ["TT" : 6]
}

如果需要查找key,则必须像您已经尝试过的那样运行整个字典,但是字典支持对键和值(如

)的快速枚举.

for (key,value) in dictionaries {
    if key.contains("FD") { //or any other checks you need to identify your key
        value["TT"] = 6
    }
}

I have this structure: [String: [String: Double]]()

Specifically, something like that: var dictionaries = ["GF": ["ET": 4.62, "EO": 21.0],"FD": ["EE": 80.95, "DE": 0.4]]

How can I easily access and modify nested dictionaries?

EXAMPLE UPDATED: I want to append "TT": 6 at FD and later I want to append another dictionary inside the array. At the end I'll print the results.

for (key,value) in dictionaries {

    // if array contains FD, add the record to FD
    if key.contains("FD") {
        dictionaries["FD"]!["TT"] = 6
    }
    else {
    // if array doesn't contain FD, add FD and add the record to it
        dictionaries = dictionaries+["FD"]["TT"] = 6 // <-- I know that it's wrong but I want to achieve this result in this case.
    }
}

Result of print will be:

GF -> ET - 4.62, EO - 21.0
FD -> EE - 80.95, DE - 0.4, TT - 6

MISSION: I need to append new dictionary records like in the example above, update existing ones in a simple and straightforward way, loop easily through records to read the values and print them out.

Can anyone help me? Never had the chance to manage dictionaries in Swift since now.

解决方案

It'll be much simpler if you have the key "FD" you can use

dictionaries["FD"]!["TT"] = 6

to add a new value ["TT":6] or modify existing value of TT to 6. Note that the ! between ["FD"]!["TT"] assumes that ["FD"] exists regardless. You need to check if ["FD"] exists otherwise. Like:

if dictionaries["FD"] != nil {
    dictionaries["FD"]!["TT"] = 6
} else {
    dictionaries["FD"] = ["TT" : 6]
}

If you need to look for the key you will have to run the entire dictionary like you already tried, but dictionaries support fast enumeration for key and values like

for (key,value) in dictionaries {
    if key.contains("FD") { //or any other checks you need to identify your key
        value["TT"] = 6
    }
}

这篇关于在Swift中轻松访问和修改字典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆