I just want to chip in to show how messy real code becomes due to protocol single conformance rule. Here is some simplified real iOS app code where a view controller calls two variants of ItemSelectorViewController class to display a picker for items of type Element. Things get very messy when the class acts as the selector's delegate to process user selections - see the workaround solution below where two artificial classes are created to deal with fact Swift won't allow two delegate conformances with different associated types. I don't propose a solution here, just want to point out real-word problems I've hit.
// Example view controller
class ExampleViewController: ViewController {
// Show category picker
func showCategoryPicker {
let vc = ItemSelectorViewController(...)
vc.setDelegate(delegate: ClassForISCategory(inner: self))
present(vc, animated: true, completion: nil)
}
// Show time picker
func showTimePicker {
let vc = ItemSelectorViewController(...)
vc.setDelegate(delegate: ClassForISTimePeriod(inner: self))
present(vc, animated: true, completion: nil)
}
}
// ItemSelectorViewControllerDelegate for category picker
class ClassForISCategory {
var inner: ExampleViewController
init(inner: ExampleViewController) {self.inner = inner}
}
extension ClassForISCategory: ItemSelectorViewControllerDelegate {
typealias Element = Category
// methods for ItemSelectorViewControllerDelegate
}
// ItemSelectorViewControllerDelegate for time period picker
class ClassForISTimePeriod {
var inner: ExampleViewController
init(inner: ExampleViewController) {self.inner = inner}
}
extension ClassForISTimePeriod: ItemSelectorViewControllerDelegate {
typealias Element = TimePeriod
// methods for ItemSelectorViewControllerDelegate
}