0

I have a two UIViewControllers A and B and I would like to have a single extension which implements a UITableViewDataSource which should be limited to above two A and B controllers. Is it possible?

I get a compiler error message for the code below:

Error message: Trailing 'where' clause for extension of non-generic type 'UIViewController'

class A: UIViewController {}
class B: UIViewController {}
extension UIViewController: UITableViewDataSource where Self: A, Self: B { }
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93

1 Answers1

0

It doesn't work like this, you have to create a protocol and apply it to your classes

protocol YourProtocol { } 

extension YourProtocol where Self: UIViewController {
 
}
extension A: YourProtocol {} 
extension B: YourProtocol {}

And in the protocol you do all your code, or simply instead of Protocol you could create a class and subclass it

Vitalii Shvetsov
  • 404
  • 2
  • 11
  • I have tried protocol YourProtocol: UITableViewDataSource { } but it leads to errors when implementing UITableViewDataSource in a YourProtocol extension – Blazej SLEBODA Aug 26 '20 at 20:05