gpt4 book ai didi

swift - 字典键是否存在?

转载 作者:搜寻专家 更新时间:2023-11-01 06:59:30 24 4
gpt4 key购买 nike

你能帮我在字典的swift中做一个扩展来查找键是否存在吗?我想在我的程序中使用这个扩展。

最佳答案

您可以使用 Dictionary 的这个扩展来检查键是否存在。

extension Dictionary {
func contain(_ key: Key) -> Bool {
return self[key] != nil
}
}

let dict = ["temp" : 2, "temp2" : false, 501 : "2"] as [AnyHashable : Any]

dict.contain("temp") // true

关于swift - 字典键是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51496618/

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