【发布时间】:2023-03-14 14:46:01
【问题描述】:
我有这个带有 2 个构造函数的小枚举。当我尝试使用Int 参数进行初始化时,我收到"Generic parameter 'T' could not be inferred" 错误。
import UIKit
public enum Result<T> {
case Success(T)
case Failure(Int)
init( any: T) {
self = .Success(any)
}
init( number: Int) {
self = .Failure(number)
}
}
let a = Result(any: "A String")
print(a)
let b = Result(number: 1)
print(b)
有没有办法从 T 中排除 Int 或以某种方式优先考虑第二个初始化程序?
在 XCode 7.3.1 上测试。
【问题讨论】:
-
编译器是对的。
Result(number: 1)中的T是什么?
标签: xcode swift generics enums