Is there a better way to do this switch?

Viewed 34

What I'm trying to do is: that based on the notification type, I'll change the image of the tableViewCell, and I know that maybe there is a better way of achieving this. I thought that maybe using enums would be a good way, but there is a lot of code in here, and it will do nothing but grow with time.

Is there a better way of achieving this?

enum NotificationIcons {
    case newPayment
    case newPaymentMethod
    case newOffers
    case userInfoUpdate
    case supportChat
    case newAnnouncement
    case cardVerification
    
    var strings: String {
        switch self {
            
        case .newPayment:
            return "new_payment"
        case .newPaymentMethod:
            return "new_payment_method"
        case .newOffers:
            return "new_offers"
        case .userInfoUpdate:
            return "user_info_update"
        case .supportChat:
            return "support_chat"
        case .newAnnouncement:
            return "new_announcement"
        case .cardVerification:
            return "card_verification"
        }
    }

    var image: UIImage {
        switch self {
        case .newPayment: return UIImage(named: "Icon-notification-service-pay")!
        case .newPaymentMethod: return UIImage(named: "Icon-notification-add-paycard")!
        case .newOffers: return UIImage(named: "Icon-notification-promos")!
        case .userInfoUpdate: return UIImage(named: "Icon-notification-update-data")!
        case .supportChat: return UIImage(named: "Icon-notification-support")!
        case .newAnnouncement: return UIImage(named: "Icon-notification-advice")!
        case .cardVerification: return UIImage(named: "Icon-notification-add-paycard")!
        }
    }
    
    var detailImage: UIImage {
        switch self {
        case .newPayment: return UIImage(named: "Icon-notification-detail-service-pay")!
        case .newPaymentMethod: return UIImage(named: "Icon-notification-detail-add-paycard")!
        case .newOffers: return UIImage(named: "Icon-notification-detail-promos")!
        case .userInfoUpdate: return UIImage(named: "Icon-notification-detail-update-data")!
        case .supportChat: return UIImage(named: "Icon-notification-detail-support")!
        case .newAnnouncement: return UIImage(named: "Icon-notification-detail-advice")!
        case .cardVerification: return UIImage(named: "Icon-notification-detail-add-paycard")!
        }
    }
    
}

Inside my tableViewCell I have this variable notification which starts to set all values on didSet

switch notification.type {
            case NotificationIcons.newPayment.strings:
                notificationImageView.image = NotificationIcons.newPayment.image
                break
            case NotificationIcons.newPaymentMethod.strings:
                notificationImageView.image = NotificationIcons.newPaymentMethod.image
                break
            case NotificationIcons.newOffers.strings:
                notificationImageView.image = NotificationIcons.newPaymentMethod.image
                break
            case NotificationIcons.userInfoUpdate.strings:
                notificationImageView.image = NotificationIcons.newPaymentMethod.image
                break
            case NotificationIcons.supportChat.strings:
                notificationImageView.image = NotificationIcons.newPaymentMethod.image
                break
            case NotificationIcons.newAnnouncement.strings:
                notificationImageView.image = NotificationIcons.newPaymentMethod.image
                break
            case NotificationIcons.cardVerification.strings:
                notificationImageView.image = NotificationIcons.newPaymentMethod.image
                break
            default:
                break
            }
1 Answers

Firstly, assign rawValue of type String to the NotificationIcons enum, like this:

enum NotificationIcons: String {
    case newPayment = "new_payment"
    //...
}

Then, modify the switch statement with an initializer:

guard let type = NotificationIcons(rawValue: notification.type) else { return }
notinotificationImageView.image = type.image
Related