gpt4 book ai didi

swift - 有没有更好的方法来进行这种切换?

转载 作者:行者123 更新时间:2023-12-04 09:35:43 25 4
gpt4 key购买 nike

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

最佳答案

首先,分配类型 String 的 rawValue到 NotificationIcons枚举,像这样:

enum NotificationIcons: String {
case newPayment = "new_payment"
//...
}
然后,使用初始化程序修改 switch 语句:
guard let type = NotificationIcons(rawValue: notification.type) else { return }
notinotificationImageView.image = type.image

关于swift - 有没有更好的方法来进行这种切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62602716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com