0
如何使用Swift函数(如过滤器)优化代码? 我想要做的是我需要找到一个特定的密钥,如果存在的话,无论该密钥的值是在下面的数组中?使用过滤器查找数组中的键Swift 3
下面的方法适用于我,但我认为有一些更好的方法来做到这一点。
let arrDic = [["Key1": "yes"], ["Key2": "no"], ["Key3": "yes"], ["Key4": "Option1, Option2"], ["Key5": "OC1_OPTIONB"], ["Key6": "H1_OPTIONA"]]
for dict in arrDic {
if dict["Key1"] != nil {
print(true)
break
}
}
比较http://stackoverflow.com/q/29679486/2976878 – Hamish
@Hamish谢谢你,这就是我一直在寻找。 –
适用于你的情况:'if arrDic.contains(其中:{$ 0 [“Key1”]!= nil})' –