我目前正在处理通知,并希望打印出一个标识符。如何用一个开关替换所有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()
}
答案 0 :(得分:2)
初始化enum
中的NotificationIdentifier
,并使用如下所示的切换用例:
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)")
}
}
答案 1 :(得分:1)
您可以像这样在Enum上使用切换用例:
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()
}
答案 2 :(得分:1)
您可以避免切换,如果出现其他类似这样的语句,则您可以在这段代码中得到 #include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
struct Params {
char* ch;
int num;
};
void* print_char(void* th_param) {
int i;
struct Params* par = (struct Params*)th_param;
for(i=0; i<par->num; i++) {
printf("%s", par->ch);
}
return NULL;
}
int main() {
struct Params* p1 = malloc(sizeof(struct Params));
p1->ch = "a"; p1->num = 5;
struct Params* p2 = malloc(sizeof(struct Params));
p2->ch = "b"; p2->num = 10;
pthread_t th_id;
pthread_create(&th_id, NULL, &print_char, p1);
pthread_t th_id2;
pthread_create(&th_id2, NULL, &print_char, p2);
fflush(stdout);
while(1) {;}
return 0;
}
的情况……
NotificationIdentifier
答案 3 :(得分:1)
如果您只想打印出一个标识符,则可以使用一个打印语句:
let notificationIdentifier = NotificationIdentifier.init(rawValue: response.notification.request.identifier) ?? .pushWithContent)
print("Handling notification with the \(notificationIdentifier.rawValue)")
这还将覆盖默认值(例如.pushWithContent
)。