objective-c 在Objective-C中等效的静态构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/992070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static constructor equivalent in Objective-C?
提问by Franklin Munoz
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?
我是 Objective C 的新手,我无法找出语言中是否有静态构造函数的等效项,即类中的静态方法,将在此类类的第一个实例之前自动调用被实例化。还是我需要自己调用初始化代码?
Thanks
谢谢
回答by Quinn Taylor
The +initializemethod is called automaticallythe first time a class is used, before any class methods are used or instances are created. You should never call +initializeyourself.
第一次使用类时会自动+initialize调用该方法,然后再使用任何类方法或创建实例。你永远不应该给自己打电话。+initialize
I also wanted to pass along a tidbit I learned that can bite you down the road: +initializeis inherited by subclasses, and is also called for each subclasses that doesn't implement an +initializeof 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:
我还想传递一个我学到的花絮,它可以让你一头雾水:+initialize由子类继承,并且还为每个没有实现+initialize自己的. 如果您天真地在+initialize. 解决方案是检查类变量的类型,如下所示:
+ (void) initialize {
if (self == [MyParentClass class]) {
// Once-only initializion
}
// Initialization for this class and any subclasses
}
All classes that descend from NSObject have both +classand -classmethods that return the Classobject. 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.
所有继承自 NSObject 的类都有返回对象的+class和-class方法Class。由于每个类只有一个 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
aClassitself) - - isKindOfClass:(Class)aClass(true for
aClassand children) - + isSubclassOfClass:(Class)aClass(same as above, but a class method)
- - isMemberOfClass:(Class)aClass(仅适用于
aClass自身) - - isKindOfClass:(Class)aClass(适用于
aClass儿童) - + isSubclassOfClass:(Class)aClass(同上,不过是一个类方法)
Edit:Check out this post by @bbumthat explains more about +initialize: http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/
编辑:查看@bbum 的这篇文章,其中解释了更多关于+initialize:http: //www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-很多/
Also, Mike Ash wrote a nice detailed Friday Q&A about the +initializeand +loadmethods:
https://www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html
另外,Mike Ash 写了一篇关于+initialize和+load方法的详细的周五问答:https:
//www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html
回答by Nathan Kinsinger
There is the +initializeclass method that will be called before a class is used.
在使用类之前将调用+initialize类方法。
回答by Richard J. Ross III
A bit of an addendum to this topic:
本主题的一些附录:
There is another way to create a 'static constructor' in obj-c, using an __attributedirective:
还有另一种方法可以使用__attribute指令在 obj-c 中创建“静态构造函数” :
// prototype
void myStaticInitMethod(void);
__attribute__((constructor))
void myStaticInitMethod()
{
// code here will be called as soon as the binary is loaded into memory
// before any other code has a chance to call +initialize.
// useful for a situation where you have a struct that must be
// initialized before any calls are made to the class,
// as they would be used as parameters to the constructors.
// e.g.
myStructDef.myVariable1 = "some C string";
myStructDef.myFlag1 = TRUE;
// so when the user calls the code [MyClass createClassFromStruct:myStructDef],
// myStructDef is not junk values.
}

