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

    <small id='fUtFg'></small><noframes id='fUtFg'>

    <tfoot id='fUtFg'></tfoot>
    1. <legend id='fUtFg'><style id='fUtFg'><dir id='fUtFg'><q id='fUtFg'></q></dir></style></legend>

          <bdo id='fUtFg'></bdo><ul id='fUtFg'></ul>

        在 Objective-C 中分配给自己

        时间:2023-09-08

          <small id='yIZRY'></small><noframes id='yIZRY'>

          <legend id='yIZRY'><style id='yIZRY'><dir id='yIZRY'><q id='yIZRY'></q></dir></style></legend>

              <tbody id='yIZRY'></tbody>
          1. <tfoot id='yIZRY'></tfoot>

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

              • <bdo id='yIZRY'></bdo><ul id='yIZRY'></ul>
                • 本文介绍了在 Objective-C 中分配给自己的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我来自 C++ 世界,所以分配 this 的想法让我不寒而栗:

                  I'm from the C++ world so the notion of assigning this makes me shudder:

                  this = new Object; // Gah!
                  

                  但在 Objective-C 中有一个类似的关键字,self,这是完全可以接受的:

                  But in Objective-C there is a similar keyword, self, for which this is perfectly acceptable:

                  self = [super init]; // wait, what?
                  

                  许多示例 Objective-C 代码在 init 例程中使用上述行.我的问题:

                  A lot of sample Objective-C code uses the above line in init routines. My questions:

                  1) 为什么分配给 self 有意义(诸如因为语言允许"之类的答案不算数)

                  1) Why does assignment to self make sense (answers like "because the language allows it" don't count)

                  2) 如果我没有在 init 例程中分配 self 会发生什么?我是否将我的实例置于某种危险之中?

                  2) What happens if I don't assign self in my init routine? Am I putting my instance in some kind of jeopardy?

                  3) 当下面的 if 语句失败时,这是什么意思,我应该怎么做才能从中恢复:

                  3) When the following if statement fails, what does it mean and what should I do to recover from it:

                  - (id) init
                  {
                      self = [super init];
                  
                      if (self)
                      {
                          self.my_foo = 42;
                      }
                  
                      return self;
                  }
                  

                  推荐答案

                  这是一个经常被新手挑战的话题:

                  This is a topic that is frequently challenged by newcomers:

                  • 威尔·希普利:self = [stupid init];
                  • Matt Gallagher:它是什么是指当你将 [super init] 分配给 self 时?
                  • Apple 文档:实现初始化程序
                  • Cocoa-Dev: self = [super init] 辩论

                  基本上,它源于这样一种想法,即超类可能已经覆盖了指定的初始化程序以返回与从 +alloc 返回的对象不同的对象.如果您没有将 super 的初始化程序的返回值分配给 self,那么您可能正在处理一个部分初始化的对象(因为 super initialized 与您正在初始化的对象不同).

                  Basically, it stems from the idea that a superclass may have over-ridden the designated initializer to return a different object than the one returned from +alloc. If you didn't assign the return value of super's initializer into self, then you could potentially be dealing with a partially initialized object (because the object that super initialized isn't the same object that you're initializing).

                  总体而言,super 很少会返回不同的内容,但在某些情况下确实会发生.

                  On the whole, it's pretty rare for super to return something different, but it does happen in a couple of cases.

                  这篇关于在 Objective-C 中分配给自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 Swift 初始化器不能在其超类上调用便利初始化器? 下一篇:如何在小部件中使用自定义字体?

                  相关文章

                • <small id='DNOHe'></small><noframes id='DNOHe'>

                • <legend id='DNOHe'><style id='DNOHe'><dir id='DNOHe'><q id='DNOHe'></q></dir></style></legend>

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

                      <tfoot id='DNOHe'></tfoot>

                      • <bdo id='DNOHe'></bdo><ul id='DNOHe'></ul>