Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing enums with arguments

Tags:

enums

ios

swift

Here are some enums I have:

enum SlideTemplate : Equatable {
    case centered(content: SlideContent)
    case horizontalSplit(leftContent: SlideContent, rightContent: SlideContent)
    case empty

    static func == (lhs: SlideTemplate, rhs: SlideTemplate) -> Bool {
        // not sure what to do here
    }
}

enum SlideContent {
    case text(content: String)
    case image(content: UIImage)
}

struct Slide {
    let template: SlideTemplate
}

Now I want to check what type of template a Slide has.

func getSlideTemplate(slide: Slide) {
    if slide.template == SlideTemplate.centered {
        print("centered")
    } else if slide.template == SlideTemplate.horizontalSplit {
        print("horizontalSplit")
    } else {
        print("empty")
    }
}

The above function obviously doesn't work. It states:

"Binary operator '==' cannot be applied to operands of type 'SlideTemplate' and '(SlideContent, SlideContent) -> SlideTemplate'"

I'm not sure how to fix this. I looked up other cases of comparing enums, but I couldn't apply those to this situation.

like image 868
Human Cyborg Relations Avatar asked Mar 18 '26 01:03

Human Cyborg Relations


1 Answers

Main idea is about comparing related values and those raw values if needed. So, you == func can be like this:

static func == (lhs: SlideTemplate, rhs: SlideTemplate) -> Bool {
    switch (lhs, rhs) {
    case let (.centered(lvalue), .centered(rvalue)):
        return lvalue == rvalue
    case let (.horizontalSplit(lleft, lright), .horizontalSplit(rleft, rright)):
        return lleft == rleft && lright == rright
    case (.empty, .empty):
        return true
    default:
        return false
    }
}

For example, if you don't need to compare raw values of .centered, you can check only related values:

...
case (.centered, .centered):
    return true
...

And as @user28434 mentioned, you should make SlideContent conforms to Equatable protocol as well.

static func ==(lhs: SlideContent, rhs: SlideContent) -> Bool {
    switch (lhs, rhs) {
    case let (.text(lcontent), .text(rcontent)):
        return lcontent == rcontent
    case let (.image(lcontent), .image(rcontent)):
        return lcontent == rcontent
    default:
        return false
    }
}

And now you can compare via == full-typed-values like:

if slide.template == SlideTemplate.centered(content: SlideContent.text(content: "SomeContent")) {
    print("centered for SomeContent")
}

To fast compare associated values you can use if case construction:

if case .centered = slide.template {
    print("centered")
}

Conclusion.

  • If you want to know "what type of SlideTemplate contains this property" - use if case .centered = slide.template { ... } way without implementing Equatable protocol.
  • If you want to fully compare SlideTemplate properties - implement Equatable protocol and compare them with ==.
like image 125
pacification Avatar answered Mar 20 '26 17:03

pacification



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!