从 NSObject 继承是如何工作的?
关于 Objective-C 有一些事情让我感到困惑:
首先,在 objective-c指南,很清楚每个类都需要调用其子类的init方法。对于直接继承自 NSObject 的类是否需要调用其 init
方法,还有些不清楚。是这样吗?如果是这样,为什么呢?
其次,在关于 NSObject 的部分中,有这样的警告:
不需要从另一个类继承任何特殊行为的类仍然应该成为 NSObject 类的子类。类的实例至少必须能够在运行时表现得像 Objective-C 对象。从 NSObject 类继承这种能力比在新的类定义中重新发明它更简单、更可靠。
这是否意味着我需要明确指定所有对象都从 NSObject 继承?或者这就像 Java/Python/C# 中所有类都是 NSObject 的子类型?如果没有,除了 NSObject 之外还有什么理由创建根类吗?
There are a couple of things about Objective-C that are confusing to me:
Firstly, in the objective-c guide, it is very clear that each class needs to call the init method of its subclass. It's a little bit unclear about whether or not a class that inherits directly from NSObject needs to call its init
method. Is this the case? And if so, why is that?
Secondly, in the section about NSObject, there's this warning:
A class that doesn’t need to inherit any special behavior from another class should nevertheless be made a subclass of the NSObject class. Instances of the class must at least have the ability to behave like Objective-C objects at runtime. Inheriting this ability from the NSObject class is much simpler and much more reliable than reinventing it in a new class definition.
Does this mean that I need to specify that all objects inherit from NSObject explicitly? Or is this like Java/Python/C# where all classes are subtypes of NSObject? If not, is there any reason to make a root class other than NSObject?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
1) 任何时候在 Objective-C 中分配一个对象时,它的内存都会被清零,并且必须通过调用
init
来初始化。NSObject
的子类可能有自己专门的init
例程,并且在开始时它们应该调用其超类的init
例程,如下所示:这个想法是所有
init
例程最终都会流入NSObject
的init
。2) 你需要明确继承:
除了 NSObject 之外,没有理由有一个根类——很多 Objective-C 严重依赖这个类,所以试图规避它会结果你不必要地搬起石头砸自己的脚。
1) Any time an object is allocated in Objective-C its memory is zeroed out, and must be initialized by a call to
init
. Subclasses ofNSObject
may have their own specializedinit
routines, and at the beginning of such they should call their superclass'init
routine something like so:The idea being that all
init
routines eventually trickle up toNSObject
'sinit
.2) You need to be explicit about the inheritance:
There is no reason to have a root class other than
NSObject
-- a lot of Objective-C relies heavily on this class, so trying to circumvent it will result in you needlessly shooting yourself in the foot.由于可以从不同的根基类继承,所以当你创建任何新类时,你必须显式声明你从 NSObject 继承(当然,除非你已经子类化了其他东西,而其他东西本身又可能是 NSObject 的子类)。
几乎从来不需要创建自己的基类,而且这样做也不容易。
Since it is possible to inherit from different root base classes, yes you must explicitly declare you inherit from NSObject when you make any new class (unless of course you are subclassing something else already, which itself in turn probably subclasses NSObject).
Almost never is there a need to make your own base class, nor would it be easy to do so.
Objective-C 可以有多个根类,因此您需要明确继承。 IIRC NSProxy 是另一个根类。您可能永远不想或不需要创建自己的根类,但它们确实存在。
至于调用NSObject的init,部分是自定义的,部分是安全的。 NSObject 的 init 现在可能不会做任何事情,这并不能保证未来的行为不会改变。为了安全起见,请调用 init。
Objective-C can have multiple root classes, so you need to be explicit about inheritance. IIRC NSProxy is another root class. You'll likely never want or need to create your own root class, but they do exist.
As for calling NSObject's init, it's part custom and part safety. NSObject's init may not do anything now, that's no guarantee that future behaviour won't change. Call init to be safe.
你需要调用[super init],因为初始化后面有一些你不必编写的代码,因为它是在 NSObjects init 中为你编写的,例如可能是实际的内存分配等。
You need to call [super init] because there is code behind initializing that you dont have to write because it is written for you in NSObjects init, such as probably actual memory allocation etc.