字典错误:成员'+'的模糊引用 [英] Dictionary error: Ambiguous reference to member '+'

查看:113
本文介绍了字典错误:成员'+'的模糊引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在swift 3方法中有以下代码:

I have the following code in a swift 3 method:

let dict = ["A": 1, "B": 2, "C": 3]
let sum = dict["A"]! + dict["B"]! + dict["C"]!

由于对成员'+'的模糊引用,代码无法编译错误。但是如果我尝试只添加两个元素,它就会按预期编译并运行。

The code does not compile because of the Ambiguous reference to member '+' error. But if I try to add only two elements it compile and works as expected.

let dict = ["A": 1, "B": 2, "C": 3]
let sum = dict["A"]! + dict["B"]!

正常工作。

我错过了什么语言应该起作用?

Am I missing something from how the language should work?

推荐答案

另一种解决方法:

let dict = ["A": 1, "B": 2, "C": 3]
let sum = 0 + dict["A"]! + dict["B"]! + dict["C"]!

这篇关于字典错误:成员'+'的模糊引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆