Black Horse programmer ___ loading and initialization of OC classes

Source: Internet
Author: User
Tags call back

Constructor

Two steps are required to create an available object.

1. Allocate storage space + alloc

2. initialize-init

Person * P1 = [person alloc]

Person * P2 = [P1 init]

It is equivalent to: person * P = [person new]. However, although this method can quickly create a new object, it cannot initialize the new object.

Sometimes all the objects we want to create have an initial value. At this time, we can rewrite the constructor.

-(ID) Init {// you must call back the super init method to initialize some member variables declared in the parent class and other attributes self = [Super init]; // If the initialization is successful, it is necessary to initialize if (self! = Nil) {_ age = 10; // make the created age attributes equal to 10} return self ;}

Notes for rewriting Constructor

1. First call the constructor of the parent class [Super init]

2. initialize the member variables in the subclass.

Supplement:

ID: a universal pointer that can point to any OC object.

Class Nature

The class itself is also an object of the class type, referred to as the class Object Class C = [person class], and uses the class to create the person object.

Class loading and initialization

When the program starts, all classes and categories in the project will be loaded, and the + load method for each class and category will be called back, and only once

When an object is created using a class for the first time, the + initialize method of the current class is called, that is, the initialization object, so that the created object can be used

Rule:

When the program starts, the system first loads the parent class and then the Child class (first calls the + load method of the parent class, and then calls the + load method of the Child class)

Initialize the parent class first, and then initialize the subclass (first call the + initialize method of the parent class, and then call the + initialize method of the subclass)

Sel

SEL is a method packaging. It encapsulates the method into a sel type data and finds the corresponding method address. You can find the method address to call the method.

[P test]-> directly call the test method

Sel S = @ selector (test)

[P performselector: S]-> indirect call of the test method

In the method, _ cmd indicates the SEL value of the current method. Therefore, using _ CMD in the method implementation will lead to an endless loop, for example:

-(Void) Test2 {// _ cmd indicates the current method nsstring * STR = nsstringfromselector (_ cmd); // it will lead to an endless loop // [self defined mselector: _ cmd]; nslog (@ "Test2 method called ----- % @", STR );}

Black Horse programmer ___ loading and initialization of OC classes

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.