【问题标题】:Is there a better way to do this switch?有没有更好的方法来做这个切换?
【发布时间】:2020-06-26 20:55:17
【问题描述】:

我想做的是:根据通知类型,我将更改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
            }

【问题讨论】:

  • 为什么是NotificationIcons.strings plual?在任何情况下,您都可以创建一个 NotificationIcon 结构,其字段是 name: Stringimage: NSImage 等,然后使用单个开关创建枚举的单个字段,它返回正确的 NotificationIcon每个给定的情况。然后,来电者可以随意询问图片或姓名。
  • 其实比枚举开关好,你可以做一个字典映射NotificationIconKey到`NoticationIcon

标签: swift enums switch-statement


【解决方案1】:

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

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

然后,用初始化器修改 switch 语句:

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

【讨论】:

  • 我有这个想法,但我不记得具体怎么做
  • 哦,好的。希望这可以帮助。如果确实如此,那么当您能够做到时,将其标记为正确。
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多