gpt4 book ai didi

swift - 快速获取字典元素编号

转载 作者:行者123 更新时间:2023-11-28 15:10:17 25 4
gpt4 key购买 nike

我有一个 [string:bool] 字典如下

["name1":true,"name2":false,"name3":true]

我想获取具有真 bool 值的键的索引号

    for(product,value) in products{
if(value == true){
print()
}
}

我该怎么做

最佳答案

对于您的问题,这可能是一种更优雅、更“swift ”的解决方案。 ForEach 是对 Swift 语言的功能补充。

let dict = ["name1": true, "name2": false, "name3": true]
dict.forEach { (product, value) in
if value == true { dict.index(forKey: key) }
}

尽管如此,您也可以在 dict 上进行过滤,以生成一个新字典,值为 true 的记录。

let trueDict = dict.filter { return $1 }

关于swift - 快速获取字典元素编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47768215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com