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

发布于 2024-07-24 05:29:27 字数 116 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清醇 2024-07-31 05:29:27

+initialize第一次使用类时,在使用任何类方法或创建实例之前,会自动调用方法。 您永远不应该自己调用+initialize

我还想传递一个我了解到的小知识,它可以让您在路上咬住:+initialize 由子类继承,并且也会为每个未实现 的子类调用>+初始化自己的。 如果您天真地在 +initialize 中实现单例初始化,这可能会特别成问题。 解决方案是检查类变量的类型,如下所示:

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

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

在切题主题上,如果您还没有学习以下相关方法,那么值得学习一下:


编辑:< /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:

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

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:


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

檐上三寸雪 2024-07-31 05:29:27

+initialize 类方法在使用类之前调用​​。

There is the +initialize class method that will be called before a class is used.

动次打次papapa 2024-07-31 05:29:27

本主题的一点补充:

还有另一种方法可以使用 __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.
}

A bit of an addendum to this topic:

There is another way to create a 'static constructor' in obj-c, using an __attribute directive:

// 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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文