UNNotificationCategory subclass init issue

354 views Asked by At

I want to subclass UNNotificationCategory(UserNotifications), because I want to use enums instead of hard coded strings as category identifiers. There is one convenience init inside UNNotificationCategory definition

public convenience init(identifier: String, actions: [UNNotificationAction], intentIdentifiers: [String], options: UNNotificationCategoryOptions = [])

I am not able to write an initializer for my subclass. I understand I cant have designated initializer inside the subclass because I want to call the convenience init of superclass. But my convenience init is also throwing complier error.

Here's the code:

enum PushNotificationCategoryIdentifier:String {
}    

convenience init(categoryIdentifier:PushNotificationCategoryIdentifier, actions:[UNNotificationAction], intentIdentifiers:[String], options: UNNotificationCategoryOptions) {
            self.init(identifier: categoryIdentifier.rawValue, actions: actions, intentIdentifiers: intentIdentifiers, options: options)
        }

This is resulting in error: self.init isn't called on all paths before returning from initializer

I guess this is because this class is implemented in Objective-C and may be they have not called the designated initailizer from convenience initailizer(as Objective-C classes dont have to call designated initializer from convenience initailizer).

But does that mean I can't subclass UNNotificationCategory if I want to write an initializer in it?

1

There are 1 answers

1
saad_nad On

No you can do this. You will have to define init() method for this. Right now you have only defined convenience init(). But you will have to define init()in your subclass. When you write a convenience init() it is only there to help initialization in an easy way but still you will have to call designated init with syntax init() from the convenience init(). You can read it on Apple Official Documentation