Ios Switch语句而不是大量if语句
我目前正在处理通知,希望打印出一个标识符。如何用一个开关替换所有if语句 以下是我的枚举,它将所有标识符与相应的字符串值保持一致:Ios Switch语句而不是大量if语句,ios,swift,if-statement,switch-statement,uilocalnotification,Ios,Swift,If Statement,Switch Statement,Uilocalnotification,我目前正在处理通知,希望打印出一个标识符。如何用一个开关替换所有if语句 以下是我的枚举,它将所有标识符与相应的字符串值保持一致: enum NotificationIdentifier:String { case local = "Local Notification" case localWithAction = "Local Notification with Action" case localWithContent = "Local Notificati
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()
}
可以在枚举上使用如下开关大小写:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.notification.request.identifier {
case .local:
print("local")
case .localWithAction:
print("localWithAction")
case .localWithAction:
print("localWithAction")
case .localWithContent:
print("localWithContent")
case .pushWithAPNs:
print("pushWithAPNs")
case .pushWithFirebase:
print("pushWithFirebase")
default:
print("pushWithContent")
}
completionHandler()
}
您可以避免像这样的switch和if-else语句。。。这段代码将为您提供
NotificationIdentifier
case
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if let notificationIdentifier = NotificationIdentifier(rawValue: response.notification.request.identifier) {
print("Handling notification with the \(notificationIdentifier.rawValue)")
switch notificationIdentifier {
case .local:
print("Handle local")
case .localWithAction:
print("Handle localWithAction")
case .localWithContent:
print("Handle localWithContent")
case .pushWithAPNs:
print("Handle pushWithAPNs")
case .pushWithFirebase:
print("Handle pushWithFirebase")
case .pushWithContent:
print("Handle pushWithContent")
}
} else {
print("cant convert to NotificationIdentifier")
}
completionHandler()
}
如果只想打印出标识符,可以使用单个打印语句:
let notificationIdentifier = NotificationIdentifier.init(rawValue: response.notification.request.identifier) ?? .pushWithContent)
print("Handling notification with the \(notificationIdentifier.rawValue)")
这也将包括默认值(例如
.pushWithContent
)。初始化通知标识符的枚举
,并使用如下开关情况:
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)")
}
}
感谢新用户提出的优秀、基本的问题。本网站欢迎基本问题,如果他们写的正确,再次感谢。