iOS Miscellaneous Pen-7 (class method The difference between load and initialize)

Source: Internet
Author: User

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

loadthe 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:

    1. All initializers in the any framework of you link to.
    2. All +load methods in your image.
    3. All C + + static initializers and C + + __attribute__(constructor) functions in your image.
    4. 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 loadmethods 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.