A brief introduction to the application of category in IOS development _ios

Source: Internet
Author: User
Tags extend inheritance instance method

To create a category file:

Here, you must choose the class of base, as follows:

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.

Take a simple example to see how category is used.

Now we have a class called MyClass

Copy Code code as follows:

#import

@interface Myclass:nsobject
-(void) myprint;
@end
#import "MyClass.h"

@implementation MyClass
-(void) myprint{
NSLog (@ "myprint called");
}
@end

It has an instance method: Myprint, later we can call it after the extension

Well, with the above MyClass, we want to add a HelloWorld method without adding a subclass, without modifying the MyClass class. Just add two files Myclass+helloworld.h and myclass+helloworld.m.

Enclose the category name in the declaration file and the implementation file by using "()". The original class name +category "This is the agreed file naming method.

Take a look at how these two files are implemented, press Command+n on Xcoed, create a new file, select Objective-c category, and Xcode will automatically help you create a file with a convention naming method.

Category on's class is MyClass, right.

So Xcode to help you create the myclass+helloworld.h and myclass+helloworld.m these two files.

So let's add a HelloWorld method now. Look at the implementation code as follows:


Copy Code code as follows:

#import "MyClass.h"

@interface MyClass (HelloWorld)
-(void) HelloWorld;
@end
#import "Myclass+helloworld.h"

@implementation MyClass (HelloWorld)
-(void) helloworld{
NSLog (@ "Hello London Olympics!");
}
@end

Called in Main
Copy Code code as follows:

MyClass *myclass = [[[MyClass alloc]init]autorelease];

[MyClass HelloWorld];
[MyClass Myprint];


Run Print results:

Copy Code code as follows:

2012-08-09 11:24:16.697 objectivec[16053:403] Hello London Olympics!

2012-08-09 11:24:16.699 objectivec[16053:403] Myprint called the


What about the use of the category:

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.