I'm trying to create a simple protocol that says whether or not an object is in an "on" state or an "off" state. The interpretation of what that is depends on the implementing object. For a UISwitch, it's whether the switch is on or off (duh). For a UIButton, it could be whether the button is in the selected state or not. For a Car, it could be whether the car's engine is on or not, or even if it is moving or not. So I set out to create this simple protocol:
protocol OnOffRepresentable {
func isInOnState() -> Bool
func isInOffState() -> Bool
}
Now I can extent the aforementioned UI controls like so:
extension UISwitch: OnOffRepresentable {
func isInOnState() -> Bool { return on }
func isInOffState() -> Bool { return !on }
}
extension UIButton: OnOffRepresentable {
func isInOnState() -> Bool { return selected }
func isInOffState() -> Bool { return !selected }
}
Now I can make an array of these kinds of objects and loop over it checking whether they are on or off:
let booleanControls: [OnOffRepresentable] = [UISwitch(), UIButton()]
booleanControls.forEach { print($0.isInOnState()) }
Great! Now I want to make a dictionary that maps these controls to a UILabel so I can change the text of the label associated with the control when the control changes state. So I go to declare my dictionary:
var toggleToLabelMapper: [OnOffRepresentable : UILabel] = [:]
// error: type 'OnOffRepresentable' does not conform to protocol 'Hashable'
Oh! Right! Silly me. Ok, so let me just update the protocol using protocol composition (after all, the controls I want to use here are all Hashable: UISwitch, UIButton, etc):
protocol OnOffRepresentable: Hashable {
func isInOnState() -> Bool
func isInOffState() -> Bool
}
But now I get a new set of errors:
error: protocol 'OnOffRepresentable' can only be used as a generic constraint because it has Self or associated type requirements
error: using 'OnOffRepresentable' as a concrete type conforming to protocol 'Hashable' is not supported
Ok... So I do some stack overflow digging and searching. I find many articles that seem promising, like Set and protocols in Swift, Using some protocol as a concrete type conforming to another protocol is not supported, and I see that there are some great articles out there on type erasure that seem to be exactly what I need: http://krakendev.io/blog/generic-protocols-and-their-shortcomings, http://robnapier.net/erasure, and https://realm.io/news/type-erased-wrappers-in-swift/ just to name a few.
This is where I get stuck though. I've tried reading through all these, and I've tried to create a class that will be Hashable and also conform to my OnOffRepresentable protocol, but I can't figure out how to make it all connect.
MythicalTypeprotocol was the only protocol he was trying to conform to. So when I try substitutingMythicalTypedirectly for myOnOffRepresentableit's not enough. I can't figure out how to jam theHashablepart into it. The same applied for Rob Napier's blog, ... maybe. HisAnimalprotocol is generic on aFoodassociatedtype, but that's not quite what I'm doing either. The realm.io examples seemed like they'd be best, butSequenceTypeis also very different from my specific needs.