在Objective-C中等效的静态构造函数? [英] Static constructor equivalent in 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
和孩子是真的) - + 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/iniailize-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屋!