如果我试着在字典中添加三个菜单,当我试图打开菜单中的第三项时,我不会让我出错,并抛出错误。但是,如果我强行拆开其中的两个,我就可以得到它们的2和。
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)"我使用的是斯威夫特3,它会不会不再支持在斯威夫特3?
发布于 2016-10-08 06:56:51
您可以始终迭代并添加到总计,比一行长加法要简单得多。
var totalCost: Double = 0
for each in menu {
totalCost += each.value
}
print(totalCost)https://stackoverflow.com/questions/39913093
复制相似问题