Juan Pablo Boero Alvarez Juan Pablo Boero Alvarez - 1 year ago 64
Swift Question

How to know super class designated initializers?

I am in need of subclassing, compiler warning says "Must call a designated initializer of the superclass", but i can not find them, where should i look for designated initializers?, i would like to know where to look for them independent of the class for future subclassing.


import Foundation
import UIKit

class JBSOSLongGestureRecognizer: UILongPressGestureRecognizer {

init() {


self.allowableMovement = 40
self.minimumPressDuration = 5
self.cancelsTouchesInView = false
self.numberOfTouchesRequired = 1


Answer Source

UILongPressGestureRecognizer inherits from UIGestureRecognizer. The designated initializer for UIGestureRecognizer is public init(target: AnyObject?, action: Selector).

Override init(target: AnyObject?, action: Selector) instead of init().

I found the designated initializer by jumping to the definition of UILongPressGestureRecognizer. I didn't see an initializer there, but I did see that it was a subclass of UIGestureRecognizer. Stepping into the UIGestureRecognizer declaration revealed the public initializer.

This information is also available in the Apple API docs. The documentation will say Designated Initializer next to the designated initializer.

UIGestureRecognizer Documentation