Special Syntax of OC, special Syntax of OC

Source: Internet
Author: User

Special Syntax of OC, special Syntax of OC
I. Category

1. What is classification?

> Category: You can expand many new methods without changing the original class, but not new member variables.

If you want to extend new member variables, consider using inheritance.

2. Writing Form of classification

> 1). Category header file:

@ Interface original class (category name)

// Method declaration

@ End

> 2) implementation file of classification:

@ Implementation Original class (category name)

// Implementation of methods in classification Declaration

@ End

3. Usage notes

Note for classification:

1), you cannot access the member variables of @ private scope in the original class primary in the category.

2) If the classification implements a method with the same name as the original class, it will overwrite the method of the original class. Yes, this method of the original class cannot be called.

3) if multiple classes implement methods with the same name as the original class, the classification method will be called first, and the final compilation has a higher priority.

(That is, classification (the final priority for compilation) --> original class --> parent class of the original Class)

Therefore, it is better to implement a method with the same name as the original class in classification.

4. Benefits of classification

1), you can develop a large project module to reduce the complexity of the project.

2), many functions are distributed to different classes and developed by different team members to enhance team cooperation.

5. Related Operations

1) In Xcode 6.3, you can use the shortcut key command + N to select the Objective-C File under OS X to create a category for the corresponding class and enter the project, if you do not like the shortcut key, you can follow File --> NEW --> File .. to select the corresponding file operations.

2) the declaration and implementation of the created category are as follows:

// Classification statement

# Import "Father. h "@ interface Father (Work) // {// int age; // The classification cannot expand the member variable //}-(void) work; //-(void) walk; @ end

// Classification implementation

# Import "Father + Work. h "@ implementation Father (Work) // original class + category name-(void) work {// NSLog (@" father whose name is % @ is working ", _ name); // you cannot directly access NSLog, a member variable in the @ private scope of the original class in the category (@ "father whose name is % @ is working", self. name) ;}- (void) walk {NSLog (@ "the name is % @ father + work walking", self. name) ;}@ end

 

Ii. Nature of Classes

1. Classes are also objects

> Actually, a Class is an object of the Class type, which can be referred to as a "Class Object" for short"

Hold down the command key in Xcode and place the mouse in the class to find the true appearance of the original class:

1 #if !OBJC_TYPES_DEFINED2 /// An opaque type that represents an Objective-C class.3 typedef struct objc_class *Class;4 5 /// Represents an instance of a class.6 struct objc_object {7     Class isa  OBJC_ISA_AVAILABILITY;8 };

The above is the definition of the Class type.

> A class name represents a Class Object. Each class has only one class object.

> Related image placement

2. + load AND + initialize

> + Load

When the program starts, it loads all classes and calls the + load method of all classes and classes.

Load the parent class first, and then load the subclass; that is, call the + load of the parent class first, and then call the + load of the subclass.

First load the original Meta class, then load the category

No matter whether this class is used during the program running, it will call + load

> + Initialize

When you use a class for the first time (such as creating an object), The + initialize method will be called once.

A class only calls the + initialize method once. It first calls the parent class and then calls

Defines an Animal class and enables Cat to inherit from Animal for demonstration + load AND + initialize

1 # import "Animal. h "2 3 @ interface Cat: Animal 4-(void) quickEat; 5 @ end 6 7 @ implementation Cat 8 + (void) load 9 {10 NSLog (@ "Cat + load"); 11} 12 13 + (void) initialize14 {15 NSLog (@ "Cat + initialize "); 16} 17 18-(void) quickEat19 {20 NSLog (@ "cat-eat"); 21} 22 23 @ end24 // output result 07:28:51. 047 03 nature and initialization of the class [1918: 162703] Cat + load 07:28:51. 047 03 nature and initialization of the class [1918: 162703] Cat + initialize

 

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.