How do I declare a variable that has a type and implements a protocol?

后端 未结 3 748
太阳男子
太阳男子 2020-11-28 10:55

My app has a protocol for detail view controllers, stating they must have a viewModel property:

protocol DetailViewController: class {
    var v         


        
3条回答
  •  庸人自扰
    2020-11-28 11:42

    Another way would be to introduce intermediate base classes for the appropriate UIKit view controllers that implement the protocol:

    class MyUIViewControler : UIViewController, DetailViewController ...
    class MyUITableViewController : UITableViewController, DetailViewController ...
    

    Then you inherit your view controllers from these view controllers, not the UIKit ones.

    This is not natural either, but it doesn't force all your UIViewControllers to satisfy the UIViewControllerInject protocol as GoZoner suggested.

提交回复
热议问题