Using delegates on generic protocol

后端 未结 2 1780
半阙折子戏
半阙折子戏 2021-01-22 23:14

I have got this code:

protocol GenericProtocol: class {
    associatedtype type
    func funca(component: type)
}

class MyType {

    weak var delegate         


        
2条回答
  •  粉色の甜心
    2021-01-22 23:40

    This is not possible to use generic protocols. Use concrete type or usual protocol instead. You have to declare concrete T outside of those abstractions

    For instance, this should work:

    class MyType {
    
        weak var delegate: UsingGenericProtocol? // 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
        typealias type = T
    
        init(t: T) {
            myType = MyType(t: t)
        }
    
        func funca(component: T) {
    
        }
    }
    
    let instance = UsingGenericProtocol(t: 0)
    

提交回复
热议问题