gpt4 book ai didi

Swift 表达式,数组中的字典为空或不存在 - 可靠地为零?

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

数组的字符串字典

blah: [String:[Stuff]]

对于给定的键,说“foo”,我想知道该数组中有多少项 - 但是,如果没有没有这样的数组,我只是想要得到

我正在做这个......

blah["foo"]?.count ?? 0

所以

if ( (blah.down["foo"]?.count ?? 0) > 0) {
print("some foos exist!!")
else {
print("there are profoundly no foos")
}

我说的对吗?

最佳答案

你是对的,但你可能会发现删除可选的更早会更容易:

(blah["foo"] ?? []).count 

if let array = blah.down["foo"], !array.isEmpty {
print("some foos exist!!")
} else {
print("there are profoundly no foos")
}

关于Swift 表达式,数组中的字典为空或不存在 - 可靠地为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41707435/

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