natecraft1 natecraft1 - 10 months ago 61
Swift Question

Compare Nested Enums

If I have

var currentState: TransformState = .Default
enum TransformState {
case Default
case Error(ErrorState)

enum ErrorState {
case Custom
case Default

var state = TransformState.Error(.Custom)

How can I check if
state == currentState


You just need to implement Equatable for TransformState

TransformState.ErrorState gets it for free, so you can use that fact in your implementations:

extension TransformState: Equatable {}
func ==(lhs: TransformState, rhs: TransformState) -> Bool {
    switch (lhs, rhs) {
    case (.Default, .Default):
        return true

    case let (.Error(lhsErrorState), .Error(rhsErrorState)):
        return lhsErrorState == rhsErrorState

        return false

Note that in Swift 3.0, you'll be able to put the operator overload inside the extension's body.