IOS Category does not overwrite the original class method, ioscategory
Original Blog, reprinted, please indicate the source
Blog.csdn.net/hello_hwc
Sum up a sentence
If you want to override methods of existing classes, consider using inheritance.
-------
Preface:
Category is a method to add methods to an existing class without inheriting or changing the existing class. It is still widely used in IOS development. For example, in CoreData, use category to add an appropriate method for the NSManagedObject class.
FAQ:
Why not directly put the method in ViewController?
If you put all these codes in the Controller, the Controller will be too bloated and difficult to maintain.
1. Introduction to Category
Classes can be built on any class without obtaining or modifying the code of the original class. The method for adding a category is accessible to both the current class and its subclass.In IOS Rumtime, there is no difference between the class method and the original class method.
Note:
Class methods and instance methods can be added. Although attributes can be added in syntax, they are not suitable for adding additional attributes because they cannot be tracked.
Category example
I want to customize a method for UIButton to achieve the desired effect of configuration.
1. Create a Category
Choose create Objective C File-> Next
Select Category, type UIButton in class, and type the desired name in File. Here is WCProgress. -> Next
Two files are generated.
UIButton + WCProgress. h
UIButton + WCProgress. m
Then define the interface in the. h file.
#import <UIKit/UIKit.h>@interface UIButton (WCProgress)-(void)WC_setUpCustomButton;;@end
Implement interfaces in The. m file
@implementation UIButton (WCProgress)(void)WC_setUpCustomButton{ [self setTitle:@"CustomButton" forState:UIControlStateNormal]; [self setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; self.backgroundColor = [UIColor yellowColor]; self.layer.cornerRadius = CGRectGetHeight(self.bounds)/2;}@end
In this way, you can use the following
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,150,25)]; [button WC_setUpCustomButton]; [self.view addSubview:button]; button.center = self.view.center;
The effect is as follows:
2. Why not rewrite the original class method?
The main reasons are as follows:
Many CocoaFramework implementations are implemented using Category. After rewriting, only one method will be executed during Runtime, and which method will be undefined.
For example, rewrite the NSString method base64EncodedString, and other CocoaFramework may also use Category to implement this method. This will cause the Undefined method to be executed during Runtime.
In addition, you should note that the Category method is named.
The general method is to add a prefix, for example
-(void)WC_setUpCustomButton;
Because the name does not conflict, the IOS version is being updated, and the SDK is also being updated. That is to say, we should try our best to ensure that the method names do not conflict in the future.
Link to the Apple documentation
Customizing Existing Classes