1.Category Introduction
Category, also known as Categories & categories & classification, is the OC-specific syntax, without modifying the original class based on the addition of new methods, a large class can be multi-person to sub-module development, help team cooperation, or the current class method to do a collation, in order to better update and maintenance
Usage of 2.Category
Naming rules: Class name + extension method, such as adding a run method to Car, then named (Car+run)
Select Objective-c file in new file to select Category
In the. h file, declare the category:
@interface the original class class name (category name)
Adding a variable to a category is not allowed
The new method must be written between @interface and @end
New Method declaration
@end
Implementation methods in. h files or. m files
@implementation the original class class name (category name)
New Method implementation
The implementation of the new method, like the implementation of the regular class, must be written between the @implementation and the @end
New method implementations can call member variables of the original class
The new method has the same name as the original class method, and the new method is called first because the original class method is overwritten
When there are multiple classifications and the names of the methods in the classification are the same, the method called is the last one to participate in the compilation
@end
3.Extendsion Introduction
Extension is a special case of category its name is anonymous (empty), and the newly added method must be implemented
(category does not have this restriction)
Usage of 4.Extendsion
Select Objective-c File Select Extendsion in new file
@interface the original class class name ()
{
Extendsion is a new private member variable that can be added
}
New method declaration (the method declared here is a private method)
@end
The declared method is then implemented in the. m file of the original class.
Classification and classification extension