Before introducing the two species method, the NSObject Class reference describes the two methods:
+ (void) initialize
The runtime sends initialize to each class-a program exactly one time just before the class, or any class that inherits From it, are sent its first message from within the program. (Thus the method never be invoked if the class was not used.) The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.
Translation: The runtime sends an initialization in one program of each class in a program, or from any class it inherits from, sends the first message in the program. (Therefore, when the class is not used, the method may never be called.) The runtime sends a thread-safe way to initialize the message. Before receiving this message, their subclass parent class.
+ (void) load
load
the message is sent to classes and categories that's both dynamically loaded and statically linked, but only if th E newly loaded class or category implements a method that can respond.
The order of initialization is as follows:
- All initializers in the any framework of you link to.
- All
+load
methods in your image.
- All C + + static initializers and C + +
__attribute__(constructor)
functions in your image.
- All initializers in frameworks so link to you.
In a custom implementation load
of your can therefore safely message other unrelated classes from the same image, and any load
methods implemented by those classes could not have run yet.
Translation: Load messages are sent to dynamically loaded and statically linked classes and categories, but only when newly loaded classes or classes implement a method that can be responded to. The order of initialization is as follows:
Link to all of your initializations in any frame.
All the load methods in your image.
All C + + Static initializers and C + + Use __attribute__ ( constructor ) function in your image
All of the initialization frames link to you.
In a custom implementation payload can be the same image as the other unrelated classes of the security message, but the implementation class of any Load method may not yet be running.
------------------------------------------------
Apple's documentation clearly shows that the difference between initialize and load is that load is called whenever a file of the class is referenced, and initialize is called before the first method of the class or its subclasses is called. So if the class is not referenced into the project, there will be no load call, but even if the class file is referenced but not used, then initialize will not be called.
Their similarities are that the method is only called once. (In fact, this is relative to the runtime, there will be further explanation behind).
The document also explicitly states the order of the method calls: The method of the parent class (superclass) takes precedence over the subclass (subclass) method, and the methods in the class take precedence over the methods in the category.
iOS Miscellaneous Pen-7 (class method The difference between load and initialize)