I came across this post while researching the same problem. It's been a few years since this was posted, so probably not helpful to the OP at this point, but will be for anyone searching this issue.
After seeing this post, I did some further research. It turns out it is in fact possible to subclass UIAlertAction with a custom init at least with the version of Swift used at the time of this writing. I'll answer as if directed to the OP anyways.
You need to define your init as a convenience init, and the one you are currently calling as super.init is also a convenience init. Only designated initializers can be called on super classes (hence the error). I also wanted to reference the UIAlertController that the action is attached to in my use case, so I have an instance variable in play too. I'm also using my own UIAlertController subclass here.
class MyAlertAction: UIAlertAction {
var alertController: MyAlertController? = nil
convenience init(
alertController: MyAlertController,
title: String?,
style: UIAlertAction.Style,
handler: ((UIAlertAction) -> Void)?
) {
// init must be called before instance variables and functions are referenced
self.init(title: title, style: style, handler: handler)
self.alertController = alertController
}
}
The reason this works is because either no designated initializers have been defined in the subclass, or all designated initializers from the superclass have been implemented by the subclass. In these two cases, all of the convenience initializers from the superclass are inherited down (except any with matching signatures in the subclass). This is the only way to call a convenience initializer from a superclass because as mentioned before, super.init is limited to designated initializers.
Because there is no implementation of any designated initializers in my example, the subclass also inherits all designated initializers from the superclass. So by not defining any designated initializers, all of the initializers from the superclass are available as if they were defined in the subclass.
There is a whole write-up on initialization in the Swift book available at https://docs.swift.org/swift-book/LanguageGuide/Initialization.html. Of particular interest in this case are these four subsections:
- Initializer Delegation for Class Types: https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID219
- Two-Phase Initialization: https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID220
- Initializer Inheritance and Overriding: https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID221
- Automatic Initializer Inheritance: https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID222