1

How can we handle the same case with different inputs from the response in Enum's

enum MyNotificationType: String, Codable {
        case practice = "push"
        case practice = "PracticeRecommendation"
        case play = "PlayRecommendation"
        case play = "pop"
    }

Edit:

I need this because I've common practice image for

push, PracticeRecommendation

and play image for

PlayRecommendation, pop

Edit 2:

enum MyNotificationType: String, Codable {
    case push = "push"
    case practice = "PracticeRecommendation"
    case play = "PlayRecommendation"
    case pop  = "pop"
}

And added a switch

private func showImage(_ type: MyNotificationType) {
        switch type {
        case .practiceRecommendation, .push:
            self.typeImgView.image = UIImage(named: "Practice")
            break
        case .play, .pop:
            self.typeImgView.image = UIImage(named: "Play")
            break
        }
    }

To deal with Images

Lion
  • 872
  • 1
  • 17
  • 43

1 Answers1

1

Hmm. You can use enums with associated types, though you wouldn't be able to statically ensure possible strings unless you change init(rawType) too. Though, I'm not sure what you are trying to achieve so there could be a different way

enum MyNotificationType{
 case practice(value: String)
 case play(value: String)

 init(rawType: String){
    //define what case based on string, do error if you can't handle or use a unknown case
 }
}