不能下标类型为“inout"的值 [String :Double](又名 inout Dictionary<String, Double>) [英] cannot subscript a value of type 'inout' [String :Double] (aka inout Dictionary<String, Double>)
本文介绍了不能下标类型为“inout"的值 [String :Double](又名 inout Dictionary<String, Double>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我尝试在字典中添加三个菜单,当我尝试强制解开菜单中的第三个项目时,我不会允许并抛出错误,该怎么办.但是,如果我强制打开其中的两个,我可以得到它们的总和
How can if i try to add three menus in the dictionary i wont let me and throws an error when i try to force unwrap the 3rd item in the menu. However if i force unwrap two of them, i can get them sum of two
var menu = ["fish": 10.99, "chips": 5.99, "kebab": 6.99]
var totalCost = menu["fish"]! + menu["chips"]! + menu["kebab"]!
print("The total cost of the three items is (totalCost)")
但是当我以这种方式尝试它时它起作用了
But when i tried it this way it worked
var menu = ["fish": 10.99, "chips": 5.99, "kebab": 6.99]
var totalCost = menu["fish"]! + menu["chips"]!
var thisCost = totalCost + menu["kebab"]!
print("The total cost of the three items is (thisCost)"
我正在使用 swift 3.难道 swift 3 不再支持它了吗?
I am using swift 3. Could it be that can no longer be supported in swift 3?
推荐答案
你总是可以迭代并添加到总数中,比一行长的添加要简单得多.
You can alway iterate through and add to total, much simpler that a line of long addition.
var totalCost: Double = 0
for each in menu {
totalCost += each.value
}
print(totalCost)
这篇关于不能下标类型为“inout"的值 [String :Double](又名 inout Dictionary<String, Double>)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文