在 init 和 dealloc 方法中有效使用访问器?

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

本文介绍了在 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 方法(因为 GC 在 iPhone 上不可用).我读过的关于初始化器和释放的所有内容都会引导我编写以下两种方法:

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?

推荐答案

据我了解,目前 10.5 中无法直接访问合成的 ivars 的行为被 Apple 认为是 bug;您应该可以直接访问它,但不能.

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.

因此,您应该能够做到:

Hence, you should be able to do:

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 方法中有效使用访问器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 init 和 dealloc 方法中有效使用访问器?

基础教程推荐