在init和dealloc方法中有效使用访问器? [英] Valid use of accessors in init and dealloc methods?

查看:95
本文介绍了在init和dealloc方法中有效使用访问器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在从多个来源(stackoverflow.com,cocoa-dev,文档,博客等)听说,在init和dealloc方法中使用访问器和设置(foo,setFoo :)是错误的" .我了解,如果这样做的话,很可能会混淆正在观察该属性的其他对象. (在此处给出了一个简单的示例)

I've heard now from several sources (stackoverflow.com, cocoa-dev, the documentation, blogs, etc) that it is "wrong" to use accessors and settings (foo, setFoo:) in your init and dealloc methods. I understand that there is there is a remote possibility of confusing other objects that are observing the property if you do so. (a simple example is given here)

但是,我不得不说我不同意这种做法,原因如下:

However, I have to say that I don't agree with this practice for the following reason:

新的Objective-C运行时(iPhone上的运行时和10.5中的64位运行时)允许您声明属性而无需声明相应的ivar.例如,以下类将在10.5或iPhone(设备,而不是模拟器)上编译就可以了:

The new Objective-C runtime (the one on the iPhone and the 64-bit runtime in 10.5) allows you to declare properties without declaring a corresponding ivar. For example, the following class will compile just fine on 10.5 or for the iPhone (device, not simulator):

@interface Foo : NSObject { }

  @property (retain) id someObject;

@end

@implementation Foo

  @synthesize someObject;

@end

理解上述内容是一个完全有效的Objective-C类,假设我决定编写一个初始化程序,并且出于内存管理的目的,我决定编写一个dealloc方法(因为iPhone上没有GC).我所读过的有关初始化程序和释放的所有内容都会导致我编写以下两种方法:

Understanding that the above is a perfectly valid Objective-C class, let's say I decide to write an initializer, and for memory management purposes, a dealloc method (since GC is not available on the iPhone). Everything I've ever read about initializers and deallocation would lead me to write the following two methods:

- (id) init {
  if (self = [super init]) {
    //initialize the value of someObject to nil
    [self setSomeObject:nil];
  }
  return self;
}

- (void) dealloc {
  //setting someObject to nil will release the previous value
  [self setSomeObject:nil];
  [super dealloc];
}

但是,根据文档和流行观点,这是错误的".所以我的问题是这样:

However, according to the documentation and popular opinion, this is "wrong". So my questions are this:

  1. 我应该如何在不使用访问器的情况下初始化someObject?您可能会说编译器(或运行时之类的东西)将确保someObject已经设置为nil,但是我相信依赖于那将是不正确的行为.在C语言中有不错的背景,由于没有正确初始化变量,我已经看到了很多错误,这似乎没什么不同.
  2. 如果不应该在dealloc方法中使用访问器,如何释放someObject?

如果这两个问题的答案都是您做不到",那么在init和dealloc方法中使用访问器怎么可能不好呢?

If the answer to either of these is "you can't", then how can it be bad to use accessors in your init and dealloc methods?

推荐答案

我知道,当前无法直接访问合成ivars的10.5行为被Apple视为错误;您应该可以直接访问它,但是不能访问.

I understand that the current 10.5 behavior under which the synthesized ivars are not directly accessible is considered by Apple to be a bug; you should be able to directly access it, but can't.

因此,您应该可以:

someObject = nil;

代替

self.someObject = nil;

同时,不使用显式ivar的唯一方法就是直接使用访问器.

In the meantime, using the accessor directly is the only way to do it without providing an explicit ivar.

更新:此错误已得到修复;您现在可以做someObject = nil就好了.

Update: This bug has been fixed; you can now do someObject = nil just fine.

这篇关于在init和dealloc方法中有效使用访问器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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