点符号dealloc? [英] Dot notation dealloc?

查看:162
本文介绍了点符号dealloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@property (copy) NSString *name;
@property (copy) NSString *orbit;
@property (copy) NSNumber *mass;
@property float surfaceTemp;
@property float rotationSpeed;

目前有这个

- (void)dealloc{
    [name release];
    name = nil;
    [orbit release];
    orbit = nil;
    [mass release];
    mass = nil;
    [super dealloc];
}

如果我使用Dot Notation(Objective-C 2.0) ?

If I write this using Dot Notation (Objective-C 2.0) Is this right?

- (void)dealloc{
    self.name = nil;
    self.orbit = nil;
    self.mass = nil;
    [super dealloc];
}

gary

推荐答案

-dealloc 中使用setter方法是不好的做法。

It's bad practice to use your setter methods in -dealloc. Use [name release] instead.

-dealloc 可能有意想不到的后果。如果使用KVO,设置属性可能会触发其他代码运行导致副作用,因为您的对象已经开始释放实例变量。即使不使用KVO,如果您的setter方法依赖于可能已经释放的其他实例变量,这可能会导致潜在的问题。

Calling setters during -dealloc may have unintended consequences. If using KVO, setting properties may trigger other code to run causing side effects because your object has already started releasing instance variables. Even when not using KVO this may cause potential problems if your setter method relies on other instance variables that may have already been released.

(更新以反映注释)

这篇关于点符号dealloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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