[Objective-C] basic concepts and usage of Category in OC

Source: Internet
Author: User

Basic concepts of Category

Encapsulation is an object-oriented feature, and OC is no exception. However, sometimes we encounter such a situation. It is better to encapsulate a class,

I don't want to touch it any more, but with the increase of program functions, I need to add a small method to that class, so we don't have to move it into that class.

To modify or define a subclass, you only need to add a Category of the class as needed.

(1) A method defined in a category will become a part of the original class, which is no different from calling other methods.

(2) by defining a category method for the parent class, its subclass also inherits these methods. If you add a category method to a subclass, the parent class does not use the category method of the subclass.

 

Application of category Method

 

  • Extension of existing classes: for example, you can extend the classes in the Cocoa touch framework. The methods you add to the classes will be inherited by the quilt class, and

    There is no difference between running and other methods.

     

    • As an alternative to subclass: you do not need to define or use a subclass. You can add a method to an existing category directly by category.
    • Classify methods in the class: Use category to divide a large class into small pieces for separate development, so as to better update and maintain the methods in the class.

      CATEGORY method limitations

       

      • You cannot add new instance variables to a category. The category does not have a location to hold instance variables. To add instance variables of a class, you can only define the subclass.
      • If the existing class methods are overwritten in the category. This may cause the break of the super message chain, because the parties in the category have a higher priority, so do not overwrite

        Methods in existing classes

        Class naming and usage

        Class naming rules: class name + extension method, such as NNString + Add;

        The class interface declaration is very similar to the class definition, but the class does not inherit the parent class. It only needs to carry a bracket to indicate the main purpose of the class.

        Declaration file:

         

        #import 
               
                @interface NSArray (Convert)+(NSMutableArray *)arrayFormNumber:(int)number;@end
               

        Implementation file:
         
        # Import "NSArray + Convert. h "@ implementation NSArray (Convert) + (NSMutableArray *) arrayFormNumber :( int) number {NSMutableArray * numberArray = [[NSMutableArray alloc] init]; while (number) {int last = number % 10; // retrieve the last digit; number = number/10; [numberArray addObject: [NSNumber numberWithInt: last];} return [numberArray autorelease];} @ end

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.