为什么在Objective-C中,我们使用self = [super init]而不是[super init]? [英] Why in Objective-C, we use self = [super init] instead of just [super init]?

查看:81
本文介绍了为什么在Objective-C中,我们使用self = [super init]而不是[super init]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一本书中,我看到如果子类覆盖了超类的方法,那么我们可能有

In a book, I saw that if a subclass is overriding a superclass's method, we may have

self = [super init];

首先,是否应该在子类的init方法中完成此操作?

First, is this supposed to be done in the subclass's init method?

第二,我想知道为什么通话不只是

Second, I wonder why the call is not just

[super init];

?我的意思是,在调用init时,内存已经由alloc分配了(我想由[Foobar alloc]分配,其中Foobar是子类的名称.因此我们不能只调用[super init]来初始化成员变量?为什么我们必须获取init的返回值并分配给self?我的意思是,在调用[super init]之前,self应该指向有效的内存分配卡盘...所以为什么要分配又要自我吗?

? I mean, at the time of calling init, the memory is allocated by alloc already (I think by [Foobar alloc] where Foobar is the subclass's name. So can't we just call [super init] to initialize the member variables? Why do we have to get the return value of init and assign to self? I mean, before calling [super init], self should be pointing to a valid memory allocation chuck... so why assigning something to self again?

(如果进行分配,[super init]不会只返回self的现有值吗?)

(if assigning, won't [super init] just return self's existing value?)

推荐答案

那么为什么将[super init]返回的值赋给self?看着 典型的初始化方法:

So why assign the value returned from [super init] to self? Looking at a typical initializer method:

 - (id)initWithString:(NSString *)aString {
     self = [super init];
     if (self)
     {
         instanceString = [aString retain];
     }
     return self; }

为什么我们在这里将[super init]分配给self?

Why do we assign [super init] to self here?

教科书的原因是因为[super init]被允许执行以下一项操作 三件事:

The textbook reason is because [super init] is permitted to do one of three things:

  1. 使用以下命令返回其自己的接收器(自指针不变) 继承的实例值已初始化.
  2. 使用以下命令返回其他对象 继承的实例值已初始化.
  3. 返回nil,表示失败.
  1. Return its own receiver (the self pointer doesn't change) with inherited instance values initialized.
  2. Return a different object with inherited instance values initialized.
  3. Return nil, indicating failure.

在第一种情况下,分配对自己和 instanceString在原始对象中设置(该行 instanceString = [保留aString];本来是 方法和结果将是相同的.)

In the first case, the assignment has no effect on self and the instanceString is set on in the original object (the line instanceString = [aString retain]; could have been the first line of the method and the result would be the same).

在第三种情况下,初始化失败.自我设置为零, 不会采取进一步措施,并且返回nil.

In the third case, the initialization has failed. self is set to nil, no further action is taken and nil is returned.

分配给自我的基本原理与第二个相关联 案例:如果返回的对象不同,我们需要:

The rationale for assigning to self is associated with the second case: if the returned object is different, we want the:

instanceString = [aString retain]; which gets converted to

self->instanceString = [aString retain]; to act on the correct value,

所以我们必须更改self的值以指向这个新对象.

so we have to change the value of self to point to this new object.

希望这对您有帮助...

hoping this helps...

来自可可与爱

这篇关于为什么在Objective-C中,我们使用self = [super init]而不是[super init]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆