Category (category)
The main thing the classification can do is to add an extension to this class even if you do not know the source code of a class.
In addition, classification ensures that your implementation class and other file areas are separated.
1 #import "UIViewController.h"
2 @interface Uiviewcontroller (CustomView)
3-(void) Extmethod;
4 @end
Add a method to a class using classification (add Methods to Classes)
By declaring an additional method in interface and defining the same name in the implementation. The name of the classification (the CustomView enclosed in parentheses) indicates that for this class (Uiviewcontroller) that is declared elsewhere, the method added here is extra, rather than indicating that it is a new class. You can not add additional member variables to a class by classification.
In implementation, the main way to refer to a header file is:
1 #import "Uiviewcontroller+customview.h"
2 @implementation Uiviewcontroller (CustomView)
3-(void) Extmethod;
4 @end
Also, although category cannot add a new member variable to a class, the category contains all the member variables of the class, even if it is @private. The category can redefine the new method, or override the inherited method.
Class extension (Extensions)
The class extension is just like the classification of Anonymous (that is, the name CustomView without that parenthesis), except that the class extension declaration must be implemented in @implementation.
Look at the code first:
Copy Code
1 @interface Myobject:nsobject
2 {
3 nsnumber* number;
4}
5-(nsnumber*) Getnum;
6 @end
7
8 @interface MyObject (Setter)
9-(void) Setnum: (nsnumber*) num;
Ten @end
11
@implementation MyObject
-(nsnumber*) Getnum
14 {
return number;
16}
Copy Code
Look at the code above, is there a problem? When the compiler compiles, this code can be compiled and passed, but when run, it will be an error. Why?
Because the Setnum method in the category is not implemented. And with the class extension to implement, see:
Copy Code
1 @interface Myobject:nsobject
2 {
3 nsnumber* number;
4}
5-(nsnumber*) Getnum;
6 @end
7
8 @interface MyObject ()//Note There's no name in the parentheses.
9-(void) Setnum: (nsnumber*) num;
Ten @end
11
@implementation MyObject
-(nsnumber*) Getnum
14 {
return number;
16}
17
(void) Setnum: (nsnumber*) num
19 {
Number = num;
21}
@end
Copy Code
Setnum must be implemented, or the compiler will warn you.
As we can see from the above, the similarity between classification and class extension is that it is possible to add an additional method to the class;
The difference is that to add additional methods, the taxonomy must declare the method in the first @interface, and provide the implementation in @implementation, otherwise the runtime will have an error. and the class extension, you add the method is a required API, if not to implement, the compiler will warn, and the declaration of this method can not be declared in the first @interface.
IOS Categories and Extensions (categories and extensions)