【发布时间】:2020-10-02 04:09:04
【问题描述】:
我目前正在处理通知并想打印出一个标识符。如何用一个开关替换所有 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"
}
还有我的委托方法:
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()
}
【问题讨论】:
-
感谢新用户提出的优秀、基本的问题。如果写得正确,本网站欢迎基本问题,再次感谢。
标签: ios swift if-statement switch-statement uilocalnotification