本文介绍了我是否需要为NSObject调用[super init]或[super initWithCoder]等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常当我从 UI 类继承时,我将调用感兴趣的超类初始化程序。但是,我不确定 NSObject 的实现细节,而且似乎在成员变量方面没有太多进展,所以我想:我是否需要调用 [super init] 如果我的子类扩展 NSObject

Typically when I subclass from a UI class I will call the superclass initializer of interest. However, I'm not sure of the implementation details of NSObject, and it seems like there's not much going on in terms of member vars, so I wonder: do I need to call [super init] if my subclass extends NSObject?

推荐答案

技术上,没有。 表示

Technically, no. The documentation for -[NSObject init] says that

因为它已被记录并且可能已经有了一堆依赖它的代码,在未来的Mac OS X版本中高度不太可能改变。

Because it is documented and there's probably already a bunch of code that relies on it, that fact is highly unlikely to change in future versions of Mac OS X.

编辑: 提出了一个我想要更加夸张的观点:不节省的总时间调用 - [NSObject init] 对于每个运行程序的人来说,如果你把类的超类更改为其他类,那么你不可能超过调试时间。比 NSObject 而忘记添加对 [super init] 的调用。

BoltClock's a Unicorn brings up a point that I would like to make more hyperbolic: the total time saved by not calling -[NSObject init] for everyone ever running your program is unlikely to ever exceed the debugging time that you'd incur if you ever change the superclass for your class to something other than NSObject and forget to add a call to [super init].

这篇关于我是否需要为NSObject调用[super init]或[super initWithCoder]等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:12