IOS-Categories and extensions (categories and extensions)

Source: Internet
Author: User

Category categories The main thing that can be done is to add extensions to this class even if you don't know the source code of a class. In addition, classification ensures that your implementation class and other file areas are separated.
1 #import "UIViewController.h" 2 @interface Uiviewcontroller (CustomView) 3 -(void) extmethod;4@end

Use classification to add a method to a class (add Methods to Classes) by declaring an extra method in interface and defining the same name in implementation. The name of the classification (the CustomView enclosed in parentheses) indicates that for this class (Uiviewcontroller) that is declared elsewhere, the method added here is extra, rather than indicating that it is a new class. You can not add additional member variables to a class by classification. In implementation, the main way to refer to a header file is:
1 #import "Uiviewcontroller+CustomView.h"2@implementation  uiviewcontroller (CustomView)  3 -(void) extmethod;4@end
Also, although category cannot add a new member variable to a class, the category contains all the member variables of the class, even if it is @private. The category can redefine the new method, or override the inherited method. The class extension class extension is Extensions like the classification of Anonymous (that is, the name CustomView without that parenthesis), except that the class extension declaration must be implemented in @implementation. Look at the code first:
1 @interfaceMyobject:nsobject2 { 3nsnumber*Number ;4 } 5-(nsnumber*) Getnum; 6 @end 7   8 @interfaceMyObject (Setter)9-(void) Setnum: (nsnumber*) num;Ten @end One   A @implementationMyObject --(nsnumber*) Getnum - { the returnNumber ; -}

Look at the code above, is there a problem? When the compiler compiles, this code can be compiled and passed, but when run, it will be an error. Why? Because the Setnum method in the category is not implemented. And with the class extension to implement, see:
1 @interfaceMyobject:nsobject2 { 3nsnumber*Number ;4 } 5-(nsnumber*) Getnum; 6 @end 7   8 @interfaceMyObject ()//notice there's no name inside the parentheses . 9-(void) Setnum: (nsnumber*) num;Ten @end One   A @implementationMyObject --(nsnumber*) Getnum - { the returnNumber ; - } -   --(void) Setnum: (nsnumber*) Num + { -Number =num; + } A @end

Setnum must be implemented, or the compiler will warn you.

As seen above, the similarity between classification and class extension is that you can add an extra method to the class, except that to add an extra method, the taxonomy must declare the method in the first @interface and provide the implementation in @implementation, otherwise the runtime will have an error. and the class extension, you add the method is a required API, if not to implement, the compiler will warn, and the declaration of this method can not be declared in the first @interface.

IOS-Categories and extensions (categories and extensions)

Related Article

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.