swift subclasses used in generics don't get called when inheriting from NSObject

前端 未结 3 1937
面向向阳花
面向向阳花 2021-01-14 20:44

Partial Solution Update at the end!

Attached is code that produces odd behavior. I copied it out of a swift playground so it should run in one fine.

3条回答
  •  日久生厌
    2021-01-14 21:02

    This is not an answer so much as a way to avoid the problem.

    In most of my code, I did not have to conform to NSObjectProtocol only Equatable and/or Hashable. I have implemented those protocols on the objects that needed it.

    I then went through my code, removed all NSObject inheritance except on those classes which inherit from an Apple protocol or object that requires it (Like UITableViewDataSource).

    The classes that are required to inherit from NSObject are Generic but they are not typically handed into other Generic classes. Therefore the inheritance works fine. In my MVVM pattern these tend to be the intermediate classes that work with view controllers to make logic like table views reusable. I have a tableController class that conforms to the UITableView protocols and accepts 3 generic viewModel types allowing it to provide the table logic for 95% of my views with no modifications. And when it needs it, subclasses easily provide alternate logic.

    This is a better strategy as I am no longer randomly using NSObject for no reason.

提交回复
热议问题