在Objective-C中,什么在main之前调用?

在Objective-C中,什么在main之前调用?例如,我假设Objective-C中的所有元类都在main之前实例化,并且它们的load / initialize方法被调用,全局常量等等.还有什么?这记录在哪里? 最佳答案 作为应用程序中任何类的一部分的加载方法(不在任何加载的框架中)将在main()之前执行.完整的执行顺序在加载文档(NSObject类引用)中给出:

The load message is sent to classes and categories that are both dynamically loaded and statically linked, but only if the newly loaded class or category implements a method that can respond.

The order of initialization is as follows:

• All initializers in any framework you link to.

• All +load methods in your image.

• All C++ static initializers and C/C++ __attribute__(constructor) functions in your image.

• All initializers in frameworks that link to you.

In addition:

• A class’s +load method is called after all of its superclasses’ +load methods.

• A category +load method is called after the class’s own +load method.

In a custom implementation of load you can therefore safely message other unrelated classes from the same image, but any load methods implemented by those classes may not have run yet.

因此,如果您向应用委托类添加负载,它将在main()之前运行.

HTH

点赞