Equating enums in swift for simple cases like this one is pretty straight forward.
But things get complicated, when the enum matures to something like this. Here you cannot use
== operator directly.
So to equate enum like this we have two options
- Use the complicated nested switch casing
- or Implement the Equatable protocol.
But using the nested switch casing has clear drawbacks. Hence we implement the Equatable protocol as we usually do for Classes.
After defining the
== function we can equate our enums.
Our enum comparison has now clarity and brevity. And most important it’s readable
For more information on comparison protocol you refer to this post written by Matt Thompson on NSHipster.