IOS design model-Category

Source: Internet
Author: User

The CategoryCategory mode is used to add methods to existing classes to expand existing classes. In many cases, Category is a better choice than creating a subclass. The newly added method will also be automatically inherited by all subclasses of the extended class. When we know that a method in an existing class has a BUG, but this class exists in the form of a library, we cannot directly modify the source code, category can also be used to replace the entity of a method in this existing class to fix bugs. However, there is no convenient way to call the replaced method entity in the existing class. Note that when you prepare Category to replace a method, you must ensure that all functions of the original method are implemented. Otherwise, such substitution is meaningless and will cause new bugs. Unlike subclass, Category cannot be used to add instance variables to extended classes. Category is usually used as a tool for organizing framework code. The purpose of Category 1. extend existing classes without creating inheritance classes. 2. simplify the development of classes (when a class requires the collaborative development of multiple programmers, Category can place the same class in different source files according to their purposes, so that programmers can develop a set of corresponding methods independently ). 3. Group common related methods. 4. It can be used to fix bugs without source code. In Obj-C, how to declare the Category extension of an existing class is as follows: [html] @ interface ClassName (CategoryName) -The statement above methodName1-methodName2 @ end is usually in. in the H file, then we. the m file implements these methods: [html] @ implementation ClassName (CategoryName)-methodName1-methodName2 @ end. We create an iOS Single View Applciation named CategoryExample. Then, create an NSString class category extension. File-> New-> File and then select Cocoa Touch Objective-C category. Name it ReverseNSString. The system will automatically generate a. h and. m files with a fixed format of ClassName + CategoryName. Declare Category to open NSString + ReverseNSString. h file, add the following code in it: [html] # import <Foundation/Foundation. h> @ interface NSString (ReverseNSString) + (NSString *) reverseString :( NSString *) strSrc; @ end implements Category NSString + ReverseNSString. method for implementing reverseString in the m file: [html] # import "NSString + ReverseNSString. h "@ implementationNSString (ReverseNSString) + (NSString *) reverseString :( NSString *) strSrc; {NSMutableString * reverse DString = [[NSMutableString alloc] init]; NSInteger charIndex = [strSrc length]; while (charIndex> 0) {charIndex --; nsange subStrRange = NSMakeRange (charIndex, 1 ); [reversedString appendString: [strSrcsubstringWithRange: subStrRange];} return reversedString;} @ end the rest of the work is to verify our Category. Add a button ReverseString to view, and set the corresponding action method to reverseString. add a label on the view and name it myString. The default value is "HelloCategory Design Pa ". Ttern !". Click the button to reverse the string. The main code is as follows: [html]-(IBAction) reverseString :( id) sender {NSString * test = [NSStringreverseString: _ myString. text]; _ myString. text = test;} Code Organization Category is used for effective decomposition of large classes. Generally, a large class method can be divided into different groups based on a certain logic or correlation. The larger the amount of code for a class, the more useful it will be to break this class into different files, each file is a collection of some related methods of this class. When multiple developers work together to complete a project, each person is responsible for the development and maintenance of a separate cagegory. In this way, Version Control is simpler, because there are fewer conflicts between developers. Category VS does not have any well-defined criteria for adding subclasses as a guide to when to use Category and when to add subclasses. But there are several guiding suggestions: to add a new variable, you need to add a subclass. If you only want to add a new method, it is better to use 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.