【发布时间】:2017-05-07 02:34:51
【问题描述】:
我想继承UNNotificationCategory(UserNotifications),因为我想使用枚举而不是硬编码字符串作为类别标识符。
UNNotificationCategory定义里面有一个方便的init
public convenience init(identifier: String, actions: [UNNotificationAction], intentIdentifiers: [String], options: UNNotificationCategoryOptions = [])
我无法为我的子类编写初始化程序。 我知道我不能在子类中指定初始化程序,因为我想调用超类的便利 init。但是我的便利初始化也引发了编译器错误。
代码如下:
enum PushNotificationCategoryIdentifier:String {
}
convenience init(categoryIdentifier:PushNotificationCategoryIdentifier, actions:[UNNotificationAction], intentIdentifiers:[String], options: UNNotificationCategoryOptions) {
self.init(identifier: categoryIdentifier.rawValue, actions: actions, intentIdentifiers: intentIdentifiers, options: options)
}
这会导致错误:self.init 在从初始化程序返回之前未在所有路径上调用
我猜这是因为这个类是在 Objective-C 中实现的,可能他们没有从便利初始化器中调用指定的初始化器(因为 Objective-C 类不必从便利初始化器中调用指定的初始化器)。
但这是否意味着如果我想在 UNNotificationCategory 中编写初始化程序,我就不能继承它?
【问题讨论】:
标签: ios swift apple-push-notifications usernotifications convenience-methods