我有 Swift 2 的问题 - XCode 版本 7.0 beta 5 (7A176x)
我有一个具有两种通用类型的枚举状态。函数printState
接受 State 参数并根据参数打印“One”或“Two”
protocol Protocol1 {
}
struct Struct1: Protocol1 {
}
protocol Protocol2 {
}
struct Struct2: Protocol2 {
}
enum State<T:Protocol1, U:Protocol2> {
case One(firstStruct: T, secondStruct:U)
case Two(secondStruct:U)
}
func printState<T:Protocol1, U:Protocol2>(state: State<T,U>) {
switch state {
case .One( _):
print("One")
case .Two( _):
print("Two")
}
}
当我如下调用 printState 时。
printState(State.One(firstStruct:Struct1(), secondStruct:Struct2()))
printState(State.Two(secondStruct:Struct2())) // This fails on compilation
在第二次调用 printState 时出现编译错误 -
错误:无法使用类型为“(secondStruct:Struct2)”的参数列表调用“Two” printState(State.Two(secondStruct:Struct2()))
如果 T 和 U 受限于类类型,则一切正常。但仅当 T 和 U 是协议类型时,我才会收到此错误。此外,我可以通过使案例二也接受协议 1 来消除此错误,但它并不真正需要它。
为什么我会收到此错误?我怎样才能使这个工作,以便案例二只接受协议 1。