I have two classes with basically identical tableViewDelegate and tableViewDataSource implementation code. In the interest of leaving the code better than I found it, I figure I should try to reduce duplication. This is proving to be quite difficult. See the example code structure below:
class A : UICollectionViewCell {
//a bunch of code
}
extension A : UITableViewDataSource, UITableViewDelegate {
//A bunch of code that is nearly identical to the other class
}
class B : UIViewController {
//a bunch of code
}
extension B : UITableViewDataSource, UITableViewDelegate {
//A bunch of code that is nearly identical to the other class
}
Both extensions use the same global variables from class A and B respectively. My initial idea was to create a superclass for class A and B that already has these delegates implemented. However, I don't think this will work because classes A and B are not extending the same class. I think I would have to go too far up the class hierarchy to find a superclass that they share.
Is there a good way to reduce this repeated code?
Thanks