IOS miscellaneous-7 (differences between class method load and initialize), iosinitialize

Source: Internet
Author: User

IOS miscellaneous-7 (differences between class method load and initialize), iosinitialize

Before introducing the two methods, the NSObject Class Reference describes the two methods:

+ (Void) initialize

The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used .) the runtime sends the initialize message to classes in a thread-safe manner. superclasses receive this message before their subclasses.

A Runtime Library sends an initialization message to a program of every class in a program, or any class inherited from it, it sends the first message in the program. (Therefore, this method may never be called when this class is not used .) Send a thread-safe method to initialize messages during running. Before receiving the message, their subclass parent class.

+ (Void) load

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

In a custom implementationloadYou can therefore safely message other unrelated classes from the same image, but anyloadMethods implemented by those classes may not have run yet.

Translation: The classes and classes that load messages are sent to dynamic loading and static links, but only when the newly loaded classes or classes implement the methods that can respond. The initialization sequence is as follows:

Link to all your initialization tasks in any framework.

All load methods are available in your images.

AllC ++Static initiators andC/c ++Use_ Attribute __(Constructor)Function in your image

All initialization frameworks are linked to you.

In a custom implementation load, other irrelevant classes of the Security message can have the same image, but the implementation classes of any load method may not have run.

------------------------------------------------

The Apple documentation clearly shows the difference between initialize and load: load is called as long as the file where the class is located is referenced, initialize is called before the first method of the class or its subclass is called. Therefore, if the class is not referenced in the project, there will be no load call. However, even if the class file is referenced but not used, initialize will not be called.

Their similarities are that a method is called only once. (In fact, this is relative to runtime and will be further explained later ).

The document also clarifies the sequence of method calls: The Superclass method takes precedence over the Subclass method, and the method in the class takes precedence over the method in the Category.

 

Related Article

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.