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?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
+initialize
第一次使用类时,在使用任何类方法或创建实例之前,会自动调用方法。 您永远不应该自己调用+initialize
。我还想传递一个我了解到的小知识,它可以让您在路上咬住:
+initialize
由子类继承,并且也会为每个未实现的子类调用>+初始化
自己的。 如果您天真地在 +initialize 中实现单例初始化,这可能会特别成问题。 解决方案是检查类变量的类型,如下所示:从 NSObject 派生的所有类都具有返回
Class< 的
+class
和-class
方法。 /代码> 对象。 由于每个类只有一个 Class 对象,因此我们确实希望使用==
运算符来测试相等性。 您可以使用它来过滤只应该发生一次的事件,而不是为给定类下面的层次结构(可能尚不存在)中的每个不同类过滤一次。在切题主题上,如果您还没有学习以下相关方法,那么值得学习一下:
aClass
本身为 true)aClass
和子级为 true)编辑:< /strong> 查看 @bbum 的这篇文章,其中解释了有关
+initialize
的更多信息:https:// web.archive.org/web/20201108095221/http://www.friday.com/bbum/2009/09/06/iniailize-can-be-execulated-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
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.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:All classes that descend from NSObject have both
+class
and-class
methods that return theClass
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:
aClass
itself)aClass
and children)Edit: Check out this post by @bbum that explains more about
+initialize
: https://web.archive.org/web/20201108095221/http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/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
有 +initialize 类方法在使用类之前调用。
There is the +initialize class method that will be called before a class is used.
本主题的一点补充:
还有另一种方法可以使用 __attribute 指令在 obj-c 中创建“静态构造函数”:
A bit of an addendum to this topic:
There is another way to create a 'static constructor' in obj-c, using an
__attribute
directive: