gpt4 book ai didi

ios - switch 语句而不是大量的 if 语句

转载 作者:行者123 更新时间:2023-12-01 15:40:26 25 4
gpt4 key购买 nike

我目前正在处理通知并想打印出一个标识符。如何用一个开关替换所有 if 语句?

这是我的枚举,它使所有标识符都具有相应的字符串值:

    enum NotificationIdentifier:String {
case local = "Local Notification"
case localWithAction = "Local Notification with Action"
case localWithContent = "Local Notification with Content"
case pushWithAPNs = "Push Notification with APNs"
case pushWithFirebase = "Push Notification with Firebase"
case pushWithContent = "Push Notification with Content"
}

还有我的委托(delegate)方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

if response.notification.request.identifier == NotificationIdentifier.local.rawValue {
print("Handling notification with the \(NotificationIdentifier.local.rawValue)")
} else if response.notification.request.identifier == NotificationIdentifier.localWithAction.rawValue {
print("Handling notification with the \(NotificationIdentifier.localWithAction.rawValue)")
} else if response.notification.request.identifier == NotificationIdentifier.localWithContent.rawValue {
print("Handling notification with the \(NotificationIdentifier.localWithContent.rawValue)")
} else if response.notification.request.identifier == NotificationIdentifier.pushWithAPNs.rawValue {
print("Handling notification with the \(NotificationIdentifier.pushWithAPNs.rawValue)")
} else if response.notification.request.identifier == NotificationIdentifier.pushWithFirebase.rawValue {
print("Handling notification with the \(NotificationIdentifier.pushWithFirebase.rawValue)")
} else {
print("Handling notification with the \(NotificationIdentifier.pushWithContent.rawValue)")
}

completionHandler()
}

最佳答案

初始化 NotificationIdentifierenum 并使用 switch case 如下:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
guard let notificationIdentifier = NotificationIdentifier(rawValue: response.notification.request.identifier) else { return }
switch notificationIdentifier {
case .local:
print("Handling notification with the \(NotificationIdentifier.local.rawValue)")
case .localWithAction:
print("Handling notification with the \(NotificationIdentifier.localWithAction.rawValue)")
case .localWithContent:
print("Handling notification with the \(NotificationIdentifier.localWithContent.rawValue)")
case .pushWithAPNs:
print("Handling notification with the \(NotificationIdentifier.pushWithAPNs.rawValue)")
case .pushWithFirebase:
print("Handling notification with the \(NotificationIdentifier.pushWithFirebase.rawValue)")
case .pushWithContent:
print("Handling notification with the \(NotificationIdentifier.pushWithContent.rawValue)")
}
}

关于ios - switch 语句而不是大量的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62342615/

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