iOS Learning notes 06-category and extension
I. Overview
A category is a way to add a new method to an existing class.
Using Objective-c's dynamic runtime allocation mechanism, the category provides a more concise way than inheritance (inheritance) to extend the class to add new methods to existing classes without having to create subclasses of the object class. You can add methods to any already existing class, including those that do not have source code (such as some framework classes).
Ii. examples
1. Declaration Category
@interface NSString (Categorydemo)
-(nsnumber*) Lengthasnumber;
@end
2. Realization Category
@implementation NSString (Categorydemo)
-(nsnumber*) lengthasnumber{
Nsuinteger length = [self length];
return ([Nsnumbernumberwithunsignedint:length]);
}
@end
3. Call:
Nsmutabledictionary *dic =[nsmutabledictionarydictionary];
[Dicsetobject: [@ "Hello" lengthasnumber]forkey:@ "Hello"];
[dic setobject: [@ "world!" Lengthasnumber] Forkey: @ "World"];
NSLog (@ "%@", DIC);
4. Printing results:
2012-07-27 17:43:32.993categorydemo[2193:f803] {
Hello = 5;
World = 6;
}
III. Limitations of the category
There are two limitations:
(1) A new instance variable cannot be added to the class, and the class has no location to hold the instance variable.
(2) name conflict, where the category has a higher precedence when the methods in the category conflict with the original class method name. The class method will completely replace the initial method so that the initial method can no longer be used.
Iv. Role of the category
There are 3 main functions of the category:
(1) The implementation of the class can be dispersed across multiple files or different frameworks for easy code management. You can also provide extensions to the framework (no source code, no modification).
(2) Create a forward reference to a private method: If a method in another class is not implemented, the compiler will error when you access the private method of another class, then use the category, declare these methods in the category (without providing a method implementation), and the compiler will no longer generate a warning
(3) Adding an informal protocol to an object: Creating a nsobject category is called "Creating an informal protocol," because it can be used as a delegate object for any class.
Iv. Expansion of extension
The following are examples of syntax for extension and category:
Extension
@interface MyObject () {
int iextension;
}
-(void) If the mainimplementation of the testinextension;//class is not implemented, there is a compile warning.
@end
Category
@interface MyObject (Categorydemo) {
int icategory; Error:ivarsmay not being placed in categories
}
-(void) testincategory;//does not force a requirement to be implemented in the class's Mainimplementation
@end
Implementation
@implementation MyObject
//
-(void) testinextension{
//
//}
//
-(void) testincategory{
//
//}
@end
The main differences between them are:
1, formally, extension is anonymous category.
2, extension in the declaration of the method needs to be implemented in Mainimplementation, category is not mandatory requirements.
3, extension can add attributes (variables), category cannot be.
iOS Learning notes 06-category and extension