Dictionary 无法为类型为';inout';[String:Double](又名inout字典<;String,Double>;)

Dictionary 无法为类型为';inout';[String:Double](又名inout字典<;String,Double>;),dictionary,swift2,swift3,xcode8,Dictionary,Swift2,Swift3,Xcode8,如果我试图在字典中添加三个菜单,我怎么能不让我这样做?当我试图强制打开菜单中的第三项时,我会抛出一个错误。然而,如果我强制展开其中的两个,我可以得到两个的和 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 \(tota

如果我试图在字典中添加三个菜单,我怎么能不让我这样做?当我试图强制打开菜单中的第三项时,我会抛出一个错误。然而,如果我强制展开其中的两个,我可以得到两个的和

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)")
但当我试着这样做的时候,它起了作用

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中不再支持该功能

您可以始终迭代并添加到总计,这比一行长加法要简单得多

var totalCost: Double = 0
for each in menu {
    totalCost += each.value
}
print(totalCost)

在第一个示例中,创建字典时使用的键与访问字典时使用的键不匹配。i、 e.“金枪鱼”!=“鱼”和“牛排”!=“烤肉串”。@JamesSnook这不是原因,我只是将其编辑回原始形式,但仍然不需要。在我看来,这是一个编译器错误,第一个片段应该可以工作,错误消息毫无意义。你应该向苹果发送一个bug报告(雷达)。我似乎记得以前有人问过这个问题(尽管我现在似乎找不到),这是一个已知的bug–请参阅。看起来与(可能重复?)相同的问题