Research on initialize Initialization Method of objective-C

Source: Internet
Author: User

This articleArticleThis article introduces the comparison between objective-C and the underlying content, mainly explaining the differences between initialize and init methods. In this article, we can not only understand what objective-C did during initialization, but also learn how to study internal methods of objective-C, in this way, you can explore deeper content on your own.

One interesting thing about objective-C is that it is very similar to C. In fact, it is a C language with some other extensions and a runtime ).

With this in every objective-CProgramWill take effect in the additional running time, give it some dynamic features. C and C ++ do not have the running time, and they are compiled to fully complyCodeIn the order of execution, there are not many.

The advantage of running time in objective-C is that it can be involved in the process of your program running. In objective-C, it includes checking whether an object can process specific messages. If it cannot be processed, it will help you automatically call other specific methods to complete the process.

Initialize is not init

One of the running time behaviors is initialize. Although it looks a bit like the common init, they are not the same.

During the program running, it calls initialize once for each class in your program. This call takes place before your class receives a message, but after its superclass receives initialize.

For example, a class called Duck:

 

# Import "duck. H "; @ implementation duck + (void) initialize {nslog (@" duck initialize ") ;}- (void) Init {nslog (@" duck init ") ;}@ end

Here we record the initialize and init call times.

We create three duck object instances:

 

 
Nslog (@ "Hello, world! "); Duck * duck1 = [[Duck alloc] init]; duck * duck2 = [[Duck alloc] init]; duck * duck3 = [[Duck alloc] init];

Take a look at the record:

[Session started at 20:03:25-0400.]

20:03:25. 869 initialize_example [30253: 10b] Hello, world!

20:03:25. 871 initialize_example [30253: 10b] duck initialize

20:03:25. 872 initialize_example [30253: 10b] duck init

20:03:25. 873 initialize_example [30253: 10b] duck init

20:03:25. 873 initialize_example [30253: 10b] duck init

We can see that although we have created three duck instances, initialize is called only once. We can also see that initialize is not called until we create a duck instance.

However, if duck has a sub-class, for example, we create a sub-class named Chicken (so weird ......) :

 

 
# Import <Cocoa/cocoa. h> # import "duck. H" @ interface chicken: Duck {} @ end

Note that the class chicken does not implement the initialize method.

If we run this program, but add a chicken instance:

 

 
Nslog (@ "Hello, world! "); Duck * duck1 = [[Duck alloc] init]; duck * duck2 = [[Duck alloc] init]; duck * duck3 = [[Duck alloc] init]; chicken * chicken = [[chicken alloc] init];}

We are looking forward to seeing four duck init calls (because we have established three duck and one chicken), but we see this situation:

[Session started at 20:13:34-0400.] 2008-03-23 20:13:34. 696 initialize_example [30408: 10b] Hello, world! 20:13:34. 698 initialize_example [30408: 10b] duck initialize2008-03-23 20:13:34. 699 initialize_example [30408: 10b] duck init2008-03-23 20:13:34. 700 initialize_example [30408: 10b] duck init2008-03-23 20:13:34. 700 initialize_example [30408: 10b] duck init2008-03-23 20:13:34. 700 initialize_example [30408: 10b] duck initialize2008-03-23 20:13:34. 701 initialize_example [30408: 10b] duck init

We can see the init of four duck and the initialize method of two duck. What's going on?

It seems that if a subclass does not implement the initialize method, the super class will call this method twice, one for itself, and one for the subclass.

We record the class name in the initialize class of duck, so that we can see more clearly:

    1. + (Void) initialize {

    2. Nslog (@ "duck initialize class: % @", [self class]);

    3. }

Now I understand:

 
[Session started at 20:21:08-0400.] 2008-03-23 20:21:08. 816 initialize_example [30513: 10b] Hello, world! 20:21:08. 818 initialize_example [30513: 10b] duck initialize class: Duck2008-03-23 20:21:08. 819 initialize_example [30513: 10b] duck init2008-03-23 20:21:08. 820 initialize_example [30513: 10b] duck init2008-03-23 20:21:08. 820 initialize_example [30513: 10b] duck init2008-03-23 20:21:08. 820 initialize_example [30513: 10b] duck initialize class: Chicken2008-03-23 20:21:08. 821 initialize_example [30513: 10b] duck init

If you want to use initialize only once to implement some work that runs independently, or if you want to implement the method that runs only once, check [self class], to determine whether it is what you want to achieve.

Original article address:

Http://kupuk.com/2008/03/23/objective-c-initialize-method/

Author: Paul Robinson

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.