Category: Adding new methods to known classes; Note: The class can only write methods, cannot write declarations and attributes, so the class can not be used as an interface
1. The class cannot add an instance variable to an existing class.
2. If the methods in the class and the method names in the existing classes conflict, the method in the class has a high precedence, and if the method name conflicts, the original method in the existing class cannot be used.
3, in the use of the purpose of the class, it is best to expand their own methods and the original method to separate.
1, the application of the category method:
1) Extend existing classes: For example, you can extend the classes in the Cocoatouch framework, the methods added in the class are inherited by the quilt class, and are not distinguished from other methods at run time.
2) as an alternative to subclasses: there is no need to define and use a subclass, you can add methods directly to existing classes through the class.
3) classify the methods in the class: Use Catigory to divide a large class into small chunks to be developed separately, so as to better update and maintain the methods in the class.
2, the limitations of the method of the category:
1) cannot add to class
New instance variable, where the class does not have a location to hold the instance variable. If you want to increase an instance variable of a class, you can only define the subclass by the way.
2) If, in the class to cover the method of the class. This causes the super message to break because the general method of the class has a higher priority, so it is generally not to overwrite the methods in the existing class.
3. Definition of Class Purpose:
Class purpose naming rules: Class name + extension method, such as "Nsstring+revert". The class intent interface declaration is very similar to the definition of a class, but the class does not inherit the parent class, only with a parenthesis indicating the primary purpose of the class purpose.
4. Examples:
Nsstring+revert.h file:
NSSTRING+REVERT.M file:
Extension: Notice the use of classes in the definition of this class to declare private methods;
(1). The role of extension is to define your own private methods.
(2). Add the class item (anonymous class) to the implementation file of your class to declare the private method. (Only available in. m files of this class).
(3). The form and the same category, do not have to create a new file, just put the class purpose interface place of the file in the class you need to extend the. m file inside.
The extension of a class can be seen as an anonymous class, and the class sometimes needs to be visible to itself, and the private method used can be declared in an extended manner, and the method defined in the extension is implemented in the @implementation code area of the class itself.
When a class name is not provided when the extension is defined, the method defined in the extension is considered to be an "must-implement" API in which case, if the method does not implement the code, then the compiler will report a warning, at which point the implementation of the method must appear in the @implementation code block of the class body.
Agreement
(1). The protocol declares a method that can be implemented by other classes. The agreement itself will not be fulfilled.
(2). The protocol is not a class, but an interface that other objects can implement.
Attention:
@required must method (default)
@optional Optional methods
In the Girl.h file
#import<Foundation/Foundation.h>#warning?????????? 1. Statement of agreement??????????@protocolmarriagedelegate <nsobject>@ Required //must method (default)-(void) makemoney;-( void) Satisfyher; @optional //Optional Method-(void) cooking ; @end @interface girl: nsobject #warning?????????? 2. Set agent properties?????????? @property (nonatomic, assign) Span class= "Hljs-keyword" >id<marriage> delegate;-(void) Buybuybuy; @end
In the girl.m file
#import "Girl.h"@implementation Girl- (void)buybuybuy{#warning ?????????? 3.代理人执行方法 ?????????? [self.delegate makeMoney];}@end
In the Boy.h file
#import <Foundation/Foundation.h>#import "Girl.h"#warning ?????????? 4.签订协议 (当前类中要实现必须方法) ??????????@interface Boy : NSObject <MarriageDelegate>@end
In the boy.m file
#import "Boy.h"@implementation Boy#warning ?????????? 6.实现协议中声明的必须方法 ??????????- (void)makeMoney{ NSLog(@"赚钱");}- (void)satisfyHer{ NSLog(@"亲吻");}@end
Protocol Demo
Protocol Demo//////////#import<Foundation/Foundation.h>#import"Girl.h"#import"Boy.h"int main (int argc,Constchar * argv[]) {//////////use proxy//////////Girl *girl = [[Girl alloc] init]; Boy *boy = [[Boy alloc] init]; #warning?????????? 5. Designated Agent?????????? Girl.delegate = boy; [Girl Buybuybuy]; //output: Make money //determine if the object follows certain protocols BOOL result = [Girl Conformstoprotocol: @protocol ( marriage)]; if (Result) {nslog (@ " Follow the marriage agreement "); }else{nslog (@ "Do not follow marriage protocol") ; } //output: Do not follow marriage protocol return 0;}
This article has compiled the brief book Luisx article and 51CTO Lan Zhang Hao's blog, and then organizes it, hoped that everybody has a new understanding to the category, the extension and the agreement.
iOS categories, extensions, and protocols