Generic Class does not forward delegate calls to concrete subclass

后端 未结 1 1221
逝去的感伤
逝去的感伤 2021-02-15 01:33

Given the following.

protocol EntityType {
    var displayString: String { get }
}

extension String: EntityType {
    var displayString: String { return self }
         


        
相关标签:
1条回答
  • 2021-02-15 02:12

    It does seem like a possible bug. My guess it that it might have something to do with the fact that Swift generics are not visible to the Objective-C runtime, so even though your methods implemented directly in GenericListViewController<Entity> are being called, there may be some buggy behavior between the Swift and Obj-C runtimes trying to figure out overrides. Definitely worth a bug report.

    I will note though, that in strict OOP abstract superclasses generally do not conform to protocols themselves, they simply provide default implementations. It's still up to the concrete subclasses to declare protocol conformance and fill in any missing implementations.

    In the case of your code above, your GenericListViewController<Entity> class should not conform to UITableViewDataSource or UITableViewDelegate. It simply provides the default method implementations that would allow a concrete subclass to conform without having to rewrite those method implementations (unless override is desired). Your StringListViewController should be the one declaring conformance to the two protocols. If you modify your code to do that, it will actually work as expected.

    This doesn't change the fact that you have probably discovered a bug in Swift / Obj-C interop, though. I believe that what you have currently should work, although it's not the strict OOP way of handling protocol conformance.

    0 讨论(0)
提交回复
热议问题