Simple analysis on the use _ios of classification category in Objective-c

Source: Internet
Author: User
Tags class definition extend instance method naming convention

No matter how perfect a class design is, it inevitably encounters an unpredictable demand, so how do you extend existing classes? Of course, inheritance is a good choice. But Objective-c provides a special way to extend the class, called catagory, to dynamically add new behavior to existing classes. This will ensure that the original base of the class, small changes can increase the required functionality. When you extend a class with category, you do not need to access its source code or create subclasses, so that we can extend the classes that the system provides. Category in a simple way, it realizes the modularization of the related methods of the class, and assigns different class methods to different classification files.
Using the taxonomy category in Object-c is a compile-time tool that allows us to extend it by adding a method to a class (but not adding new instance variables through category), and we do not need to access the code in the class. This is a bit like using a stereotype in JavaScript to define attributes.
We can create a new method for a class without having to edit the class definition in code.
Here is the definition and use of the classification of the example program, through the following code, we can add to the object-c nsstring camelcasestring classification, using the Camelcasestring method, you can remove a string of spaces, and rewrite the words after the original space into uppercase (the string is converted to a hump).
Take a simple example to see how category is used.

Copy Code code as follows:

#import <Foundation/Foundation.h>

/*
The process of defining a taxonomy can be roughly divided into the following steps:
The first step is to create a new file with an interface that creates an existing class

The second step is to add a new file to the implementation of the methods and methods that need to be expanded, that is, the classification to be added
*/
NSString represents the name of the class that will be added to the category, which must already exist.
CamelCase is the name of the method added for the class.
Only methods can be added and variables cannot be added.
Header file naming convention: classname+categoryname.h
@interface NSString (CamelCase)

-(nsstring*) camelcasestring;

@end

@implementation NSString (CamelCase)

-(nsstring*) camelcasestring
{
Call the NSString internal method to get the hump string.
Self points to the class that is added to the category.
NSString *castr = [self capitalizedstring];

Create an array to filter out spaces and combine characters with delimiters.
Nsarray *array = [Castr componentsseparatedbycharactersinset:
[Nscharacterset Whitespacecharacterset]];

The character output of the array
NSString *output = @ "";
For (NSString *word in array)
{
output = [Output STRINGBYAPPENDINGSTRING:WORD];
}

return output;

}

@end
int main (int argc, const char * argv[])
{

NSAutoreleasePool * Pool = [[NSAutoreleasePool alloc] init];

NSString *str = @ "My name is Bill.";
NSLog (@ "%@", str);
str = [STR camelcasestring];
NSLog (@ "%@", str);

[Pool drain];
return 0;
}

What about the use of the category scene:
1, the class contains a number of methods implemented, and these methods require the members of different teams to implement
2, when you are using classes in the underlying class library, you do not want to inherit these classes and just want to add some methods.

Category can achieve the above requirements, of course, there are the use of category is needed to pay attention to the problem:
1, category can access the original class of instance variables, but can not add instance variables, if you want to add a variable, it is through inheritance to create subclasses to implement.
2, category can overload the original class method, it is not recommended to do so, this will overwrite the original class method. If you do want to overload, you can do this by inheriting the subclass.
3, and the common interface is different, in the category implementation of the instance method as long as you do not call it you can not implement all the declarations of all methods.

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.