A. Classification:
1, the scope of application when you have encapsulated a class (also may be the system class, the third party libraries), do not want to change the class, but with the increase in the function of the program need to add a method in the class, then we do not have to modify the main class, just give you the original class to add a classification. Splitting a large class into different classifications and implementing a class declaration method in different classifications allows the implementation of a class to be written into multiple. m files for easy management and collaborative development. The methods in the classification can only be declared, not implemented, so when the protocol does not support optional methods (the protocol now supports optional methods), the classification is usually used as an informal protocol. 2. Syntax in the syntax format file:
1 @interface Main class class name (class name) 2//cannot define member properties 3 @end4 5 @implementation Main Class class name (class name) 6 7 @end
The file name is usually: Main class name + class name
When calling a method, you only need to send a message to the main class reference 3. Precautions
- The method in the classification is higher than the method in the original class, that is, the method in the original class is overridden in the classification, then the method in the classification overrides the method in the original class.
- Only methods can be declared in a taxonomy, and attribute variables cannot be added, and methods in the run-time classification do not differ from those in the main class.
- In general, classifications are defined in. h files, but can also be defined in. m files, at which point the method of classification becomes a private method
4, how to use the definition of Xyzpopviewcontroller class classification
1 xyzpopviewcontroller+catcontroller.h File 2 @interfaceXYZPopViewController (Catcontroller) 3-(void) test; 4 @end 5 6 xyzpopviewcontroller+catcontroller.m file 7 @implementationXYZPopViewController (Catcontroller) 8-(void) Test {9 NSLog (@ "Testing the Xyzpopviewcontroller Classification");}11 @end
5. Although you cannot define member properties in a category, you can also let it support adding properties and member variables
Category is a common grammatical feature in objective-c, which makes it easy to add functions to existing classes. However, category does not allow new attributes or member variables to be added to existing classes.
A common approach is to access and generate association objects through Objc_getassociatedobject/objc_setassociatedobject in Runtime.h. This method is used to simulate the build properties.
1//nsobject+indiebandname.h2 @interface NSObject (indiebandname) 3 @property (nonatomic, strong) NSString * Indiebandname;4 @end
Above is the header file declaration, the following implementation of the. m file 1//NSOBJECT+INDIEBANDNAME.M 2 #import "Nsobject+extension.h"
3 #import <objc/runtime.h> 4 static const void *indiebandnamekey = &IndieBandNameKey;
8-(NSString *) Indiebandname {
If the property value is a non-ID type, the ID object of OC can be constructed by the property value first, and the non-ID type property is obtained through the object 9 return Objc_getassociatedobject (self, indiebandnamekey); 10} One-(void) Setindiebandname: (NSString *) indiebandname{
If the property value is a non-ID type, the ID object of OC can be constructed by the property value first, and the non-ID type property is obtained through the object
13
@end
Second, expand
1, the scope of application extension is a special form of classification. 2. The syntax format @interface the main class name () @end extension is usually defined in the main class. m file, and the methods declared in the extension are implemented directly in the. m file of the main class. 3. Precautions
- You can declare an instance variable in an extension, you can declare a property
- Because extensions are usually defined in the. m file of the main class, the methods and properties of the extended declaration are usually private
4, classification and extension of the distinguished classification is not to declare instance variables, usually public, the file name is: The main class name + class name. h extension is the instance variable that can be declared, is private, the file name is: The main class name _ extension identity. h, #import the header file in the. m file of the main class 5.
How to useDefines how the Xyzpopviewcontroller class is extended by 1, defined as a separate file
1 xyzpopviewcontroller_exviewcontroller.h file 2 #import "XYZPopViewController.h" 3 4 @interface Xyzpopviewcontroller () 5 @ Property (Nonatomic,strong) Nsstring*stringofex;6-(void) testex;7 @end
Mode 2, defined in the. m file of the main class
1 xyzpopviewcontroller.m file 2 #import "XYZPopViewController.h" 3 4 @interface Xyzpopviewcontroller () 5 @property ( Nonatomic,strong) nsstring*stringofex;6-(void) testex;7 @end
Methods for implementing extensions in the. m file of the main class
1 @implementation XYZPopViewController2-(void) testEx {3 [email protected] "Assign value to the attribute string defined within the extension"; 4 NSLog (@ " The properties defined by string are:%@ ", Self.stringofex); 5}6 @end
iOS Classification class extension