在Objective C中的对象分配和init [英] Object allocate and init in Objective C

查看:176
本文介绍了在Objective C中的对象分配和init的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下2种方式分配和初始化对象之间的区别是什么?

  AController * tempAController = [[AController alloc] init]; 
self.aController = tempAController;
[tempAController release];

  self.aController = [[AController alloc] init]; 

大多数apple示例使用第一种方法。为什么要分配,init和object然后立即释放?

解决方案

每个对象都有引用计数。



假设属性被声明为 @property(retain)



您的第一个示例,逐行:


  1. alloc ,它的引用计数为1。

  2. 对象被移交给 self ' setAController:方法,它发送一个 retain 知道对象来自哪里),将其引用计数增加到2。

  3. 调用代码不再需要对象本身,因此调用 release

    规则是,如果你创建一个对象,你负责释放它,当你完成它。在您的示例中,代码在设置属性后使用tempAController完成。



    重要的是记住这一点,这是setter方法的责任,调用 retain 在Objective-C中的 self.property = foo; 实际上只是 [self setProperty:foo]; setProperty:方法将根据需要保留或复制对象。



    如果属性声明为 @property(copy),那么该对象将被复制而不是保留。在第一个例子中,原始对象将被立即释放;在第二个例子中,原始对象的引用计数将是1,即使它应该是0.所以你仍然想以相同的方式写你的代码。



    如果属性被声明为 @property(assign),则 self 不声明对象的所有权,保留它。在这种情况下,第一个示例将是不正确的。这些属性很少,通常只用于对象委托。


    What is the difference between the following 2 ways to allocate and init an object?

    AController *tempAController = [[AController alloc] init];
    self.aController = tempAController;
    [tempAController release];
    

    and

    self.aController= [[AController alloc] init];
    

    Most of the apple example use the first method. Why would you allocate, init and object and then release immediately?

    解决方案

    Every object has a reference count. When it goes to 0, the object is deallocated.

    Assuming the property was declared as @property (retain):

    Your first example, line by line:

    1. The object is created by alloc, it has a reference count of 1.
    2. The object is handed over to self's setAController: method, which sends it a retain message (because the method doesn't know where the object is coming from), incrementing its reference count to 2.
    3. The calling code no longer needs the object itself, so it calls release, decrementing the reference count to 1.

    Your second example basically does steps 1 and 2 but not 3, so at the end the object's reference count is 2.

    The rule is that if you create an object, you are responsible for releasing it when you're done with it. In your example, the code is done with tempAController after it sets the property. It is the setter method's responsibility to call retain if it needs that object to stick around.

    It's important to remember that self.property = foo; in Objective-C is really just shorthand for [self setProperty:foo]; and that the setProperty: method is going to be retaining or copying objects as needed.

    If the property was declared @property (copy), then the object would have been copied instead of retained. In the first example, the original object would be released right away; in the second example, the original object's reference count would be 1 even though it should be 0. So you would still want to write your code the same way.

    If the property was declared @property (assign), then self isn't claiming ownership of the object, and somebody else needs to retain it. In this case, the first example would be incorrect. These sorts of properties are rare, usually only used for object delegates.

    这篇关于在Objective C中的对象分配和init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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