Categories and extensions of iOS development
Category:
In ios project development, you can use a category to add a new method to an existing class without creating a subclass. By using categories, we can dynamically add new methods to existing classes and modularize the definition of classes to multiple related files.
Benefits of using categories:
I. Modular Design
Ii. Use category to call private methods
The Interface format is as follows:
@ Interface existing class (category name)
// Method definition
.....
@ End
Note: Only new methods are defined in a category.
The syntax format of category implementation is as follows:
@ Implmentation existing class (category name)
// Method implementation
@ End
Create an old class first
Then a new category, such
Methods are created in the old class and new class respectively, and finally the method for calling the main. m function is as follows:
Extension
Extensions are similar to categories. extensions are equivalent to anonymous categories in the following format:
@ Interface existing class ()
// Method definition
.....
@ End
The difference between category and extension is:
Classes generally do not add variables again. They only add new methods, but can be extended.
The extension is only implemented in the interface file (. h) but not the method defined in the file (. m ).