有没有更好的方法来进行此切换?

时间:2020-06-26 20:55:17

标签: swift enums switch-statement

我想做的是:根据通知类型,我将更改tableViewCell的图像,并且我知道也许有更好的方法可以实现此目的。我认为也许使用enums是个好方法,但是这里有很多代码,它只会随着时间的增长而无济于事。

是否有更好的方法来实现这一目标?

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")!
        }
    }
    
}

在我的tableViewCell中,我有这个变量通知,它开始在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 个答案:

答案 0 :(得分:2)

首先,将String类型的rawValue分配给NotificationIcons枚举,如下所示:

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

然后,使用初始化程序修改switch语句:

guard let type = NotificationIcons(rawValue: notification.type) else { return }
notinotificationImageView.image = type.image
相关问题