【发布时间】:2018-04-28 11:20:52
【问题描述】:
我有这个代码:
protocol GenericProtocol: class {
associatedtype type
func funca(component: type)
}
class MyType<T> {
weak var delegate: GenericProtocol? // First error
var t: T
init(t: T) {
self.t = t
}
func finished() {
delegate?.funca(component: t) // Second error
}
}
class UsingGenericProtocol: GenericProtocol {
let myType: MyType<Int>
typealias type = Int
init() {
myType = MyType<Int>(t: 0)
}
func funca(component: Int) {
}
}
我想使用具有给定类型的委托。代码无法编译,因为我收到了这个错误:
Protocol 'GenericProtocol' 只能用作通用约束 因为它有 Self 或关联的类型要求
还有:
成员 'funca' 不能用于协议类型的值 '通用协议';改用通用约束
我可以通过从协议中删除类型,将其设为 Any 然后将其转换为正确的类型来省略此错误,但我认为泛型应该涵盖此问题。有什么办法可以编译这段代码吗?谢谢。
【问题讨论】: