Bugrym
Bugrym

Reputation: 85

Switch statement instead of a ton of if statements

I'm currently working with notifications and wanna print out an identifier. How can I replace all if-statements with one switch?

Here is my enum that keeps all identifier with corresponding string value:

    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"
}

And my delegate method:

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()
}

Upvotes: 3

Views: 185

Answers (4)

Frankenstein
Frankenstein

Reputation: 16361

Initialize the enum of NotificationIdentifier and use switch case like below:

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)")
    }
}

Upvotes: 2

pawello2222
pawello2222

Reputation: 54621

If you want to just print out an identifier you can use a single print statement:

let notificationIdentifier = NotificationIdentifier.init(rawValue: response.notification.request.identifier) ?? .pushWithContent)
print("Handling notification with the \(notificationIdentifier.rawValue)")

This will cover the default value (e.g. .pushWithContent) as well.

Upvotes: 1

Jawad Ali
Jawad Ali

Reputation: 14417

You can avoid switch and if else statements like this... you get NotificationIdentifier case with this piece of code ...

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()
}

Upvotes: 1

Keshu R.
Keshu R.

Reputation: 5223

You can use switch case on Enum like this:

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()
}

Upvotes: 1

Related Questions