Category
@interface ClassName (CategoryName)
Methoddeclarations
@end
category is used very frequently in iOS development . < Span style= "color: #ff0000;" In particular, when it comes to extending the system classes, we can add methods to the system classes without inheriting the system classes, maximizing the dynamic language characteristics of objective-c .
Precautions
1: A category is an extension of a class , only methods can be declared, and variables cannot be declared. Also, class methods have higher precedence and can override the original class method.
2: The Super method cannot be called in a class method. --The limitations of categories
3: Thecategory method cannot be overridden in other category methods in the same class. Because they do not anticipate their load precedence, they may have errors at compile time.
Use Category:
1. Provides the extension of the class to the framework (no source code, cannot be modified).
2. If you do not want to generate a new subclass, such as an extension to Nsarray.
3. Easy to do project management, can be a source in multiple places to share or do the method version management, multi-person collaborative development, with the local version of the replacement of the public version of the implementation.
Extended:
1: In some cases, we need to declare a @property, which is read-only (ReadOnly) and internally readable (ReadWrite), which can be achieved by extensions
@interfaceBaseclass:nsobject@property (READONLY) NSString *privatestring;@end//. m@interface BaseClass () @property (readwrite) NSString *< Span style= "margin:0px; padding:0px; font-family: ' Courier new '; >privatestring; @end @implementation Baseclass @synthesize Privatestring;//... @end
2: Enables the editor to better validate private methods or variables of a class
@interface void @end @implementation Baseclass//... @end
&NBSP;
iOS categories and extensions-Chaoxiaopo