iOS design mode--category

Source: Internet
Author: User

Original: http://blog.csdn.net/lovefqing/article/details/8289851

What is category

The category pattern is used to add methods to existing classes to extend existing classes, and in many cases the category is a better choice than creating subclasses. The newly added method will also be automatically inherited by all subclasses of the extended class. When you know that a method in an existing class has a bug, but the class is in the form of a library, and we cannot modify the source code directly, the category can also be used to replace the entity of a method in the existing class to fix the bug. However, there is no convenient way to call the existing class of the original is replaced by the method entity. It is important to note that when you are ready to have a category to replace a method, be sure to implement all the functions of the original method, otherwise this substitution is meaningless and will cause new bugs. Unlike subclasses, category cannot be used to add instance variables to an extended class. Category is often used as a tool for organizing framework code.

Use of category

1. Implement an extension of an existing class without creating an inherited class.

2. Simplify the development of the class (When a class requires multiple programmers to co-develop, the category can put the same class in different source files according to the purpose, thus facilitating the programmer to independently develop the appropriate collection of methods).

3. Group the commonly used related methods.

4. In the absence of source code can be used to fix the bug.

Usage of category

In Obj-c, the way to declare a category extension for an existing class is as follows:

[HTML]View Plaincopy
    1. @interface ClassName (CategoryName)
    2. -methodname1
    3. -methodname2
    4. @end

The above declarations are usually in. h files, and then we implement these methods in the. m file:

[HTML]View Plaincopy
    1. @implementation ClassName (CategoryName)
    2. -methodname1
    3. -methodname2
    4. @end

We created an iOS single View applciation named Categoryexample. Then create a category extension for the NSString class. File->new->file then select Cocoa Touch objective-c category. Named Reversensstring. The system automatically generates a fixed format classname+ CategoryName. h and. m files.

Declaring category

Open the Nsstring+reversensstring.h file and add the following code inside:

[HTML]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. #import <foundation/foundation.h>
    2. @interface NSString (reversensstring)
    3. + (nsstring*) reversestring: (nsstring*) strsrc;
    4. @end

Implementing category

The ReverseString method is implemented in the NSSTRING+REVERSENSSTRING.M file:

[HTML]View Plaincopy
  1. #import "Nsstring+reversensstring.h"
  2. @implementationNSString (reversensstring)
  3. + (nsstring*) reversestring: (nsstring*) strsrc;
  4. {
  5. nsmutablestring *reversedstring =[[nsmutablestring alloc]init];
  6. Nsinteger charIndex = [strsrc length];
  7. while (CharIndex > 0) {
  8. charindex--;
  9. Nsrange substrrange =Nsmakerange (charIndex, 1);
  10. [Reversedstring Appendstring:[strsrcsubstringwithrange:substrrange]];
  11. }
  12. return reversedstring;
  13. }
  14. @end


The rest of the work is to verify our category, add a button reversestring to the view, and set the corresponding action method to reversestring. Add a label to the view, named MyString, and the default value is "Hellocategory Design pattern!". Click the button to reverse the string. The main code is as follows:

[HTML]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. -(Ibaction) ReverseString: (ID) Sender {
    2. NSString *test = [Nsstringreversestring:_mystring.text];
    3. _mystring.text = test;
    4. }
Code Organization

Category is used for large class efficient decomposition. Usually a large class method can be decomposed into different groups according to some logic or correlation, the larger the code size of a class, the more useful it is to decompose the class into different files, each of which is a collection of some related methods of the class.

When multiple developers work together on a project, each individual is responsible for the development and maintenance of a separate cagegory. This makes versioning easier because there is less work conflict between developers.

Category vs Add sub-class

There is no well-defined criterion to be used as a guideline for when to use categories to add subclasses. However, there are several guiding suggestions:

    1. If you need to add a new variable, you need to add a subclass.
    2. If you just add a new method, using category is the better choice.

If there are any errors in this article, welcome to make bricks, common progress, thank you!

iOS design mode--category

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.