The depth and classification of OC language classes

Source: Internet
Author: User

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".

Class object re-run is always present
A class object is a data structure that stores basic information about a class: The class size, the class name, the version of the class, and the mapping table for the message and function.
The information held by the class object is determined at compile time and loaded into memory the first time the class is used
Class objects represent classes, classes represent class objects, class methods belong to class objects
If the recipient of the message is the class name, the class name represents the class object
At run time, all instances of the class are produced by the class object, and the class object modifies the instance's ISA value to its own address, and each instance of Isa points to the class object of that instance.
From the class object, you can know the parent information, the method that can respond, etc.
Class objects can only use class methods, not instance methods

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

Class C = [Person 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 class]; A pointer to the object of the class calls the class method

(2) class C1=[person class]; To call the class method with its name

Note: C and C1 print out the same address, class C2=[p class]; 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.

The depth and classification of OC language 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.