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

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

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

      Objective-C中的静态构造函数等效?

      时间:2023-09-08

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

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

            <tbody id='n2X1q'></tbody>

            1. <legend id='n2X1q'><style id='n2X1q'><dir id='n2X1q'><q id='n2X1q'></q></dir></style></legend>
              • <bdo id='n2X1q'></bdo><ul id='n2X1q'></ul>
              • 本文介绍了Objective-C中的静态构造函数等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我是 Objective C 的新手,我无法找出语言中是否存在等效的静态构造函数,即类中的静态方法,将在第一个实例之前自动调用此类的实例化.还是我需要自己调用初始化代码?

                I'm new to Objective C and I haven't been able to find out if there is the equivalent of a static constructor in the language, that is a static method in a class that will automatically be called before the first instance of such class is instantiated. Or do I need to call the Initialization code myself?

                谢谢

                推荐答案

                +initialize 方法在第一次使用类时被自动调用, 在使用任何类方法或创建实例之前.你不应该自己调用 +initialize.

                The +initialize method is called automatically the first time a class is used, before any class methods are used or instances are created. You should never call +initialize yourself.

                我还想传递我学到的一个花絮:+initialize 被子类继承,并且还为每个未实现的子类调用一个自己的+initialize.如果您天真地在 +initialize 中实现单例初始化,这可能会特别成问题.解决方案是检查类变量的类型,如下所示:

                I also wanted to pass along a tidbit I learned that can bite you down the road: +initialize is inherited by subclasses, and is also called for each subclasses that doesn't implement an +initialize of their own. This can be especially problematic if you naively implement singleton initialization in +initialize. The solution is to check the type of the class variable like so:

                + (void) initialize {
                  if (self == [MyParentClass class]) {
                    // Once-only initializion
                  }
                  // Initialization for this class and any subclasses
                }
                

                所有从 NSObject 派生的类都有返回 Class 对象的 +class-class 方法.由于每个类只有一个 Class 对象,我们确实想用 == 运算符测试相等性.您可以使用它来过滤应该只发生一次的事情,而不是为给定类下的层次结构中的每个不同类(可能尚不存在)过滤一次.

                All classes that descend from NSObject have both +class and -class methods that return the Class object. Since there is only one Class object for each class, we do want to test equality with the == operator. You can use this to filter what should happen only once ever, versus once for each distinct class in a hierarchy (which may not yet exist) below a given class.

                关于一个切题,如果您还没有学习以下相关方法,则值得学习:

                On a tangential topic, it's worth learning about the following related methods, if you haven't already:

                • - isMemberOfClass:(Class)aClass(仅适用于 aClass 本身)
                • - isKindOfClass:(Class)aClass(aClass 和子级为 true)
                • + isSubclassOfClass:(Class)aClass(同上,不过是类方法)
                • - isMemberOfClass:(Class)aClass (true only for aClass itself)
                • - isKindOfClass:(Class)aClass (true for aClass and children)
                • + isSubclassOfClass:(Class)aClass (same as above, but a class method)

                查看 @bbum 的这篇文章,其中解释了有关 +initialize 的更多信息:http://www.friday.com/bbum/2009/09/06/initialize-can-be-executed-multiple-times-load-not-so-much/

                Check out this post by @bbum that explains more about +initialize: http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/

                此外,Mike Ash 写了一篇关于 +initialize+load 方法的详细的周五问答:https:///www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html

                Also, Mike Ash wrote a nice detailed Friday Q&A about the +initialize and +load methods: https://www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html

                这篇关于Objective-C中的静态构造函数等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么在 Swift 类中使用必需的初始化器? 下一篇:在 Swift 初始化期间调用实例方法

                相关文章

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

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

                    <tfoot id='hokd5'></tfoot>

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