ios – Swift – 如何禁止初始化程序?
发布时间:2020-12-14 17:46:26 所属栏目:百科 来源:网络整理
导读:考虑以下具有委托的控制器类: @objc protocol FooControllerDelegate {}@objc class FooController: UIViewController { var delegate: FooControllerDelegate init(delegate: FooControllerDelegate) { self.delegate = delegate super.init(nibName: nil,
考虑以下具有委托的控制器类:
@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(编码器:),并且隐式禁止它. 解决方法
>如果禁止使用除您之外的所有指定初始值设定项的目标,则此时此时没有语言功能.这适用于各种方法.
>如果目标是每次添加自定义初始化程序时避免使用init(编码器:)的空覆盖,那么请考虑便捷关键字. Swift的安全范例假定该类要么添加“额外的”init,要么必须修改所有必需的初始化程序的行为. “Automatic Initializer Inheritance” (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- C语言的fork函数在Linux中的进程操作及相关面试题讲解
- flex 非模态的titlewindow,点击titlewindow外部时的事件
- c# – 如果我有Dispose方法,我必须实现IDisposable吗?
- c# – 如何在树视图中获取所有扩展节点?
- 如何解决“ruby安装缺少心理(对于YAML输出).”在CentOS上?
- ORACLE时间函数(SYSDATE)深入理解
- c# – DB ConnectionState = Open但context.SaveChanges抛出
- Micro2440 Nand Flash存储操作之写
- C99将作为文字初始化值传递的值定义为结构,失败为非const
- Oracle11G_归档和日志文件