natecraft1 natecraft1 - 1 year ago 95
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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download