简体   繁体   中英

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.

code:

import Foundation
import UIKit

class JBSOSLongGestureRecognizer: UILongPressGestureRecognizer {

    init() {

        super.init()

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

}

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

UILongPressGestureRecognizer is a concrete subclass of UIGestureRecognizer . So while you can subclass it using the method @JAL provided it is not intended to be subclassed.

In your code it doesn't seem like you are adding any functionality to your JBSOSLongGestureRecognizer , why not just use UILongPressGestureRecognizer and configure it with the values you want?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM