分配和初始化它们实际上是做什么的 [英] alloc and init what do they actually do

查看:97
本文介绍了分配和初始化它们实际上是做什么的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释一下Obj-C中的init和alloc做什么.我正在阅读这本obj-c书,其中提供了创建对象的示例,但它并没有真正涉及其作用的细节.分配返回什么?初始化返回什么?

Can someone explain to me what init and alloc do in Obj-C. I am reading this obj-c book that gives an example of creating object but it does not really go into details of what it does. What does alloc return? what does init return?

Animal * k = [Animal alloc];
k = [k init];

推荐答案

  • alloc分配一块内存来保存对象,并返回指针.

    • alloc allocates a chunk of memory to hold the object, and returns the pointer.

      MyClass* myObj = [MyClass alloc];
      

      myObj尚未使用,因为其内部状态未正确设置.因此,请勿编写这样的代码.

      myObj cannot be used yet, because its internal state is not correctly setup. So, don't write a code like this.

      init设置对象的初始条件并返回它.请注意,返回的内容 [a init] 可能与a不同.这说明了Yannick编写的代码:

      init sets up the initial condition of the object and returns it. Note that what's returned by [a init] might be different from a. That explains the code Yannick wrote:

      -init{
           self=[super init]; // 1.
           if(self){          // 2.
               ....
           }
           return self;       // 3.
      }
      

      1. 首先,您需要调用超类的init,以设置超类的实例变量,等等.这可能会返回不等于原始self的值,因此您需要将返回的内容分配给self.
      2. 如果self为非nil,则表示由超类控制的部分已正确初始化.现在执行初始化.所有实例变量都设置为nil(如果是对象)和0(如果是整数).您需要执行其他初始设置.
      3. 返回设置self.返回的self可能与分配的内容不同!因此,您需要将init的结果分配给变量.
      1. First, you need to call the superclass's init, to setup the superclass's instance variables, etc. That might return something not equal to the original self, so you need to assign what's returned to self.
      2. If self is non-nil, it means the part controlled by the superclass is correctly initialized. Now you perform your initialization. All of the instance variables are set to nil (if it's object) and 0 if it's integer. You'll need to perform additional initial settings.
      3. Return the set-up self. The returned self might be different from what's allocated! So, you need to assign the result of init to your variable.

    • 这建议一个重要的教训:永远不要将呼叫拆分为allocinit.不要写:

      This suggestions an important lesson: never split the call to alloc and init. Don't write:

       MyClass* myObj = [MyClass alloc];
       [myObj init];
      

      因为[myObj init]可能会返回其他内容.不要试图通过写来解决这个问题:

      because [myObj init] might return something else. Don't try to get around this by writing:

       MyClass* myObj = [MyClass alloc];
       myObj=[myObj init];
      

      因为您最终将忘记在第二行中编写myObj=部分.

      because you will eventually forget to write the part myObj= in the second line.

      始终写:

       MyClass* myObj = [[MyClass alloc] init];
      

      我也不建议写:

       MyClass* myObj = [MyClass new];
      

      ,因为它没有正确调用初始化方法:某些类不接受普通的init.例如,NSView需要initWithFrame:,而new不能调用.因此,也不要使用new.

      because it does not correctly call the initialization method: some classes doesn't accept a plain init. For example, NSView needs initWithFrame:, which can't be called with new. So, don't use new either.

      这篇关于分配和初始化它们实际上是做什么的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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