IOS category do not rewrite the original class method

Source: Internet
Author: User

Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc

Sum up a sentence

If you want to override the methods of an existing class, consider using inheritance.

-------
Preface:
Category is a way to add a method to an existing class without inheriting or altering an existing class. In the development of iOS, the use is still very gf. For example, in CoreData, use category to add the appropriate method for the Nsmanagedobject class.

FAQ:
Why not just put the method in the Viewcontroller?
Because if you put all this code in the controller, it can make the controller too bloated and difficult to maintain.

Introduction to a Category

Categories can be based on any class without having to obtain or modify the code of the original class. The methods added to the category are accessible to both the current class and subclass. in iOS rumtime, there is no difference between the methods of a category and the methods of the original class.

Attention:
Categories can add class methods and instance methods. Although adding attributes is syntactically supported, it is not appropriate to add additional properties because these additional properties cannot be traced.

Category examples
I want to customize a method for UIButton to get the effect you want to configure.

1 Creating Category
Select New Objective C File--Next

Select category, type the name you want in class Uibutton,file, I am wcprogress here. ->next

will generate two files

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

implementing interfaces in. m files

 @implementation UIButton (wcprogress)(void) wc_setupcustombutton{[ Selfsettitle:@"CustomButton"Forstate:uicontrolstatenormal]; [ Selfsettitlecolor:[UicolorBluecolor] Forstate:uicontrolstatenormal]; Self. BackgroundColor= [UicolorYellowcolor]; Self. Layer. Cornerradius= Cgrectgetheight ( Self. Bounds)/2;}@end

In this way, it can be used in the following places

UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,150,25)];    [button WC_setUpCustomButton];    [self.view addSubview:button];    button.centerself.view.center;

The effect is as follows

Second, why not rewrite the original class method

The main reasons are as follows:

Many of cocoaframework are implemented in category, and after rewriting, there is only one method that will be executed at runtime and which will be executed as undefined.

For example, one method of overriding NSString is base64encodedstring, while other cocoaframework may use category to implement this method, which results in runtime Which method to execute is undefined.

In addition, there is a place to note that is the name of the category method.
The usual way is to add a prefix, such as

-(void)WC_setUpCustomButton;

Because, now the name does not conflict, iOS version is updated, the SDK is also updated, that is, to try to ensure that the name of future methods do not conflict.

Attach links to Apple documentation

customizing Existing Classes

IOS category do not rewrite the original class method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.