我想在 Swift 中实现一个带有两个参数的泛型类。一个必需的约束是第一个参数是一个协议(protocol),第二个参数必须实现这个协议(protocol)。
代码:
protocol Protocol {}
class Generic<T: Protocol, U> where U: T {
}
编译器错误说:
error: type 'U' constrained to non-protocol, non-class type 'T'
但这不是真的,因为 T
是一个协议(protocol)。
您知道如何实现吗?
最佳答案
我认为你想要的是
protocol Protocol {}
class Generic<T: Protocol> {
init(type: T.Type, instance: T) {
}
}
但是用例可以帮助我们理解您要实现的目标...
关于ios - 实现另一个参数的 Swift 泛型类参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51375864/