考虑以下具有委托的控制器类:
@objc protocol FooControllerDelegate { } @objc class FooController: UIViewController { var delegate: FooControllerDelegate init(delegate: FooControllerDelegate) { self.delegate = delegate super.init(nibName: nil,bundle: nil) } // TODO: How do we forbid this init? required init(coder aDecoder: NSCoder) { // TODO: Fails to compile. super.init(coder: aDecoder) } }
是否有任何方法禁止使用-initWithCoder:等效,而不会使委托隐式解包,并在方法中放置一个断言(false)?
理想情况下,根本不需要为每个子类编写init(编码器:),并且隐式禁止它.