<legend id='AvvmL'><style id='AvvmL'><dir id='AvvmL'><q id='AvvmL'></q></dir></style></legend>
    <bdo id='AvvmL'></bdo><ul id='AvvmL'></ul>

    1. <small id='AvvmL'></small><noframes id='AvvmL'>

      <tfoot id='AvvmL'></tfoot>

      1. <i id='AvvmL'><tr id='AvvmL'><dt id='AvvmL'><q id='AvvmL'><span id='AvvmL'><b id='AvvmL'><form id='AvvmL'><ins id='AvvmL'></ins><ul id='AvvmL'></ul><sub id='AvvmL'></sub></form><legend id='AvvmL'></legend><bdo id='AvvmL'><pre id='AvvmL'><center id='AvvmL'></center></pre></bdo></b><th id='AvvmL'></th></span></q></dt></tr></i><div id='AvvmL'><tfoot id='AvvmL'></tfoot><dl id='AvvmL'><fieldset id='AvvmL'></fieldset></dl></div>

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

        Valid use of accessors in init and dealloc methods?(在 init 和 dealloc 方法中有效使用访问器?)
          <tbody id='XJX2I'></tbody>
      2. <i id='XJX2I'><tr id='XJX2I'><dt id='XJX2I'><q id='XJX2I'><span id='XJX2I'><b id='XJX2I'><form id='XJX2I'><ins id='XJX2I'></ins><ul id='XJX2I'></ul><sub id='XJX2I'></sub></form><legend id='XJX2I'></legend><bdo id='XJX2I'><pre id='XJX2I'><center id='XJX2I'></center></pre></bdo></b><th id='XJX2I'></th></span></q></dt></tr></i><div id='XJX2I'><tfoot id='XJX2I'></tfoot><dl id='XJX2I'><fieldset id='XJX2I'></fieldset></dl></div>

        <legend id='XJX2I'><style id='XJX2I'><dir id='XJX2I'><q id='XJX2I'></q></dir></style></legend>
              <bdo id='XJX2I'></bdo><ul id='XJX2I'></ul>
              • <small id='XJX2I'></small><noframes id='XJX2I'>

              • <tfoot id='XJX2I'></tfoot>

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Auto Layout still required after executing -layoutSubviewsquot; with UITableViewCell subclass(“执行 -layoutSubviews 后仍需要自动布局带有 UITableViewCell 子类)
                  How do I animate constraint changes?(如何为约束更改设置动画?)
                  How to change the preferred language of an iPhone app in iOS 4(如何在 iOS 4 中更改 iPhone 应用程序的首选语言)
                  NSLocale currentLocale always returns quot;en_USquot; not user#39;s current language(NSLocale currentLocale 总是返回“en_US;不是用户当前的语言)
                  How can I wait for a NSURLConnection delegate to finish before executing the next statement?(在执行下一条语句之前,如何等待 NSURLConnection 委托完成?)
                  handle when callback to a dealloced delegate?(何时回调已解除分配的委托?)

                    <tfoot id='49tmU'></tfoot>
                    • <bdo id='49tmU'></bdo><ul id='49tmU'></ul>
                      <i id='49tmU'><tr id='49tmU'><dt id='49tmU'><q id='49tmU'><span id='49tmU'><b id='49tmU'><form id='49tmU'><ins id='49tmU'></ins><ul id='49tmU'></ul><sub id='49tmU'></sub></form><legend id='49tmU'></legend><bdo id='49tmU'><pre id='49tmU'><center id='49tmU'></center></pre></bdo></b><th id='49tmU'></th></span></q></dt></tr></i><div id='49tmU'><tfoot id='49tmU'></tfoot><dl id='49tmU'><fieldset id='49tmU'></fieldset></dl></div>
                      <legend id='49tmU'><style id='49tmU'><dir id='49tmU'><q id='49tmU'></q></dir></style></legend>

                    • <small id='49tmU'></small><noframes id='49tmU'>

                          <tbody id='49tmU'></tbody>