Objective-C中的静态构造函数等效? [英] Static constructor equivalent in Objective-C?

查看:28
本文介绍了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 (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中的静态构造函数等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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