In OC we often have to extend the functionality for a class, with the following ideas:
Subclass: Create A. Class directly inherits from the original class and expands the new functionality in the class, which can either extend the method or augment the instance variable. However, you want to use an expanded function, must make?? The object of the subclass, the object of the original class has no expansion function.
Modify Source code: This is the most direct way to add functionality, but limited to the need to have the source code of the class , we have the right to modify. The method can either be expanded or the instance variable can be expanded.
Protocol: This is the way of indirect expansion of the protocol, only a bunch of methods in the declaration, so that when the class is required to obey the protocol, implement the method in the protocol to expand the function, and can only expand the method, can not expand the instance variable . limited to having the source code for that class .
Category: Class, for non-source code class extension functions, such as the system provided by the Nsstring,nsarray class, we can not get their source code, but we want to add functionality for these classes, such as printing, compared to the count, we could use the category , to augment the method, but not to extend the instance variable .
EXtension: extension, very simple, for we know the source code of the class, add private instance variables and methods, specifically declared in the. m file. Other classes cannot invoke instance variables and methods that access the private, and can only be used in. m files of that class.
Extension of the class