Li Hongqiang in-depth and classification of OC language classes developed by iOS

Source: Internet
Author: User

The depth and classification of OC language classes

First, classification

(i) basic knowledge of classifications

Concept: Category classification is the language specific to OC and depends on the class.

The function of classification: Add some methods to the class without changing the original class content.

Add a Category:

File structure diagram:

Add a method to the taxonomy

Implementation of study method

Test procedure:

(ii) Attention to the use of classifications

(1) Classification can only add methods (including class methods and Object methods), cannot increase member variables

(2) The member variables in the original class can be accessed in the implementation of the classification method;

(3) Classification can be re-implemented in the original class method, but will overwrite the original method, resulting in the original method can no longer use (warning);

(4) Priority of method invocation: Classification---The original class--parent class, if it contains more than one classification, then the final participation in the compilation of the classification takes precedence;

(5) In many cases, it is often possible to add categories to the system's own classes, such as NSObject and nsstring, because sometimes the system classes may not meet our requirements.

(6) In large-scale applications, the corresponding function is usually written in a classification, can have unlimited classification, the original class to expand, General sub-module write, a module a classification.

(iii) Classification programming exercises

(1) Adding a class method to the NSString class to calculate the number of Arabic numerals in a string object;

(2) Add an object method to the NSString class to calculate the number of Arabic numerals in the current string object;

Declaration of methods in classification

Implementation of methods in classification

Test procedure:

In-depth study of class Ii.

(a) The nature of the class

The class itself is also an object, an object of class type, or "Class object".

Definition of class type:

Typedef struct obj class *class;

The class name represents the class object, and each class has only one class object.

Create a person class with class

Create an object of person type with person

Person *p=[[person alloc] init];

There are two ways to get a class object in memory:

(1) class C=[p claa];//pointer to the object of the class.

(2) class C1=[person class];//Call the class method using the classes name

Note: C and C1 print out the same address, class c2=[p Claa]; You can prove that all objects share a class method.

(ii) Loading and initialization of classes

Test procedure:

1. When the program starts, it loads all classes and classifications in the project, and the +load method of each class and classification is called once after loading;

2. When a class is used for the first time, the +initialize method of the current class is invoked;

3. Load the parent class first, then load the subclass (call the +load method of the parent class first, then call the +load method of the subclass, and finally call the +load method of the taxonomy), initialize the parent class first, then initialize the subclass (call the +initialize method of the parent class first, and then call the subclass's + Initialize method).

4. Note: At initialization time, if the +initialize method is overridden in the taxonomy, the parent class is overwritten.

5. Override the +initialize method to listen for class usage.

Li Hongqiang in-depth and classification of OC language classes developed by iOS

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.