The Category is translated as a directory [system class extension]
(1) Implementation of extension methods beyond inheritance
(2) How to Use Category
(3) string flip method Category Extension
(4) private functions
(1) Implementation of extension methods beyond inheritance
Expand some methods in a class
1. You can divide the implementation of a class into several different files.
<1> c/c ++ cannot implement classes. Classes are implemented in several files.
<2> Expand some methods in the existing class.
In the past, we used to implement some methods in the inheritance method. Now we only need to expand some methods in the string class. [Dynamically add some methods in the system class]
<3> companies often ask what is the difference between Category and inheritance?
Category cannot completely replace inheritance, but it can do a lot of things that inheritance cannot do. It is easier to write than inheritance, but it is easier to use.
2. Each Category is part of the class.
3. Different categories of classes can be compiled separately (different developers can be responsible for a Category)
4. If you put a Category class (declaration and implementation) in A. m file, the Category cannot be accessed from outside. In this way, the private function similar to class in c ++ is implemented.
5. Category is actually an extension of the class.
Disadvantages of Category:
Only functions, messages, character segments, and variables can be extended. [Therefore, inheritance cannot be replaced.]
Category naming rules:
General:
Class name to be extended + extension variable. [h/m]
For example:
NSString + ReverseString. h
NSString + ReverseString. m
Unichar = unsigned short two bytes.
"% C" is a big C, which is different from c in speech.
AppendString is added to the string.
Implementation of. m function privatization [only calls to. m function]
@ Interface Foo (Private) // such declaration-(void) test2; @ end @ implementation Foo-(void) test {[self test2];}-(void) test2 {NSLog (@ "test2 is calling");} @ end
(2) How to Use Category
(3) string flip method Category Extension