在dealloc中使用self.property = nil有什么问题吗? [英] Is there any problem using self.property = nil in dealloc?

查看:189
本文介绍了在dealloc中使用self.property = nil有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道声明的属性会生成访问器方法,该方法有时只是语法糖.

I know declared property generates accessor method which is someway just syntax sugar.

我发现很多人在dealloc方法中使用self.property = nil.

I found quite a lot people use self.property = nil in their dealloc method.

1)在Apple的内存管理文档中,p23 它说:

1) In Apple's Memory Management document, p23 It says:

您不应该使用访问器方法设置实例变量的唯一地方是init方法和dealloc.

The only places you shouldn’t use accessor methods to set an instance variable are in init methods and dealloc.

为什么不应该呢?

2)在苹果公司的 Objective-C 2.0 中,p74

2) In apple's Objective-C 2.0, p74

已声明的属性从根本上代替了访问器方法的声明;合成属性时,编译器只会创建任何不存在的访问器方法.与dealloc方法没有直接交互-属性不会自动为您释放.但是,声明的属性确实提供了一种有用的方法来交叉检查dealloc方法的实现:您可以在头文件中查找所有属性声明,并确保释放未标记为assign的对象属性,并且标记为assign的标记不会被释放.

Declared properties fundamentally take the place of accessor method declarations; when you synthesize a property, the compiler only creates any absent accessor methods. There is no direct interaction with the dealloc method—properties are not automatically released for you. Declared properties do, however, provide a useful way to cross-check the implementation of your dealloc method: you can look for all the property declarations in your header file and make sure that object properties not marked assign are released, and those marked assign are not released.

注意:通常,在dealloc方法中,应直接release对象实例变量(而不是调用set访问器并将nil作为参数传递),如本示例所示:

Note: Typically in a dealloc method you should release object instance variables directly (rather than invoking a set accessor and passing nil as the parameter), as illustrated in this example:

- (void)dealloc { [property release]; [super dealloc]; }

但是,如果您正在使用现代运行时并合成实例变量,则无法直接访问该实例变量,因此必须调用accessor方法:

If you are using the modern runtime and synthesizing the instance variable, however, you cannot access the instance variable directly, so you must invoke the accessor method:

- (void)dealloc { [self setProperty:nil]; [super dealloc]; }

注释是什么意思?

我发现[property release];[self setProperty:nil];都可以工作.

推荐答案

设置属性可能导致通知发送到正在观察该属性的其他对象.反过来,这可能导致那些对象尝试对您的对象做进一步的处理.如果您正处于取消分配的过程中,那么这可能不是您想要的.因此,通常直接直接release相关实例变量更为安全.

Setting a property can lead to notifications being sent to other objects that are observing that property. That could in turn lead to those objects attempting to do something further with your object. If you are in the middle of deallocating, this is probably not what you want to happen. So in general it is safer to release the relevant instance variable directly.

请注意,此类问题只会在某些情况下出现,因此通常很可能在dealloc中使用self.property=nil编写代码,并且一切正常.这不是最佳实践.

Note that this sort of problem will only arise in certain cases, so it is often perfectly possible to write code using self.property=nil in dealloc and for everything to work out fine. It's just not best practice.

在Objective-C现代运行时"中,可以声明属性而无需指定ivar.运行时将综合存储,以与综合访问器一起使用.在这种情况下,您不能直接释放ivar,因为就您的代码而言,没有一个.因此,您别无选择,只能走self.property=nil路线.

In the Objective-C "modern runtime", it is possible to declare properties without ever specifying the ivar. The runtime will synthesise the storage to go along with the synthesised accessors. In this case you cannot release the ivar directly because as far as your code is concerned there isn't one. So you have no choice but to go the self.property=nil route.

这篇关于在dealloc中使用self.property = nil有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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