【发布时间】:2021-05-27 00:34:07
【问题描述】:
你能在下面的代码中解决这个Ambiguous use of 'y' 错误吗?
let x = 1
func y(_ x: Int) -> Int {
return x
}
func y(_ x: Int?) -> Int? {
return x
}
let z: Int? = y(x)
OptionalOverload.playground:5:6: note: found this candidate
func y(_ x: Int) -> Int {
^
OptionalOverload.playground:9:6: note: found this candidate
func y(_ x: Int?) -> Int? {
^
我只是希望编译器在这种情况下选择func y(_ x: Int) -> Int 方法,因为x 不是可选的。
【问题讨论】:
-
我通过从函数签名之一“func y(x: Int) -> Int”中删除_来修复它,但实际上我也想知道详细原因。
标签: swift overriding optional