Swift delegate for a generic class

后端 未结 3 1381
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 09:23

I have a class that needs to call out to a delegate when one of its properties changes. Here are the simplified class and protocol for the delegate:

protocol MyC         


        
3条回答
  •  时光取名叫无心
    2021-02-05 09:37

    It is hard to know what the best solution is to your problem without having more information, but one possible solution is to change your protocol declaration to this:

    protocol MyClassDelegate: class {
        func valueChanged(genericClass: MyClass)
    }
    

    That removes the need for a typealias in the protocol and should resolve the error messages that you've been getting.

    Part of the reason why I'm not sure if this is the best solution for you is because I don't know how or where the valueChanged function is called, and so I don't know if it is practical to add a generic parameter to that function. If this solution doesn't work, post a comment.

提交回复
热议问题