protocol CellActionHandling {
func didTapOptionButton()
func didTapEditButton()
func notActiveViewAction(state: Cell.State)
}
So I had a delegate protocol (for a cell) with three methods, it was being implemented by two classes so that those classes can respond to actions. But one class only needed two methods instead of three, so I did interface segregation, now I have two protocols, and one class conforms to both while other just to one.
protocol CellActionHandling {
func didTapOptionButton()
func didTapEditButton()
}
protocol NotActiveViewActionHandling {
func notActiveViewAction(state: Cell.State)
}
Earlier I had one protocol and was using that as the type of cell’s delegate property.
var delegate: CellActionHandling?
Problem: After interface segregation now I’m not sure what should be the type of delegate property.
var delegate: ?
I know about protocol extensions, that way I won’t be able to enforce the the conforming classes to provide implementation. And then this won’t be any different from what was being done previously.
Would really appreciate any help.
2
Answers
I think there are two different solutions here. The first solution is that in the Cell class, you have two properties for each of the different delegates. This makes sense if the two different protocols are in fact very different.
The other solution that I’d recommend is by combining the two protocols again and making the unused function optional. This makes it so when you are implementing the protocol, you don’t actually need to create a function for it. Say for example it is
NotActiveViewActionHandling
that is only being implemented once. Then you can do:If all these functions make sense in one protocol, then I’d definitely go for the second option. Since you know what
notActiveViewAction
does, you can make the decision about whether they should exist in the same protocol or not.I think you have to make two different properties for each protocol because each one has a different task. If you ever and I’m sure you’ve noticed that either UITableview or UICollectionview, both have two different type of protocols like (UICollectionViewDelegate,UICollectionViewDataSource) or (UITableviewDelegate and UITableviewDatasource) and you have to implement both or only one if you need it.