Difference between initialize, init, and load methods and when to call initializeinit

Source: Internet
Author: User

Difference between initialize, init, and load methods and when to call initializeinit

Initialize is not init
When initialize is called for the first time, for example, [[class alloc] init] will call the initialize method once. No matter how many times this class is created, it will only call this method once, we use it to initialize static variables. The init method calls this init method as long as this class is called. This class is called several times, this init method will be called several times. When a class inherits this class and is a subclass of this class, when a subclass is called, for example, if the subclass is [[class alloc] init], the initialize and init methods of the parent class are called once,
Init inherits from the NSObject root class. All subclasses do not need to override this instance method function. Of course, they can also override the init instance method in their own classes.
1-(id) init
2 {
3 if (self = [super init])
4 {
5 // Class-specific initializations
6}
7 return self;
8}
First, you must call the init Function Method of the parent class, and then write some special initialization operations (5th rows) in the if statement block ).
You can provide more parameters to quickly assign values to objects during instantiation. Rewrite init to make the created object ready. Without rewriting, you also need to assign values to the attributes of the object.
  • The load method is called when the class is loaded, that is, when the ios app starts, all classes are loaded, and the + load method of each class is called.
  • If no operation is performed on the class, the + load method is executed by default and is executed before the main function. The program is called before it starts.

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.