Create a category file:

Here, be sure to choose the base class, as follows:

No matter how perfect a class is designed,Will inevitably encounter unforeseen demand,So how do you extend an existing class?Of course, inheritance is a good choice.But objective-c provides a special way to extend classes,Called catagory, you can dynamically add new behavior to existing classes.This can ensure that the original class is based on the originalMinor changes can add needed functionality.When extending a class using category,No need to access its source code,No need to create subclasses,This way we can extend the classes provided by the system.category uses the simple way,Implements the modularization of related methods of the class,Assign different class methods to different classification files.

Take a simple example to see how category is used.

Now we have a class called myclass

@interface myclass:nsobject
-(void) myprint;
#import "myclass.h"
@implementation myclass
-(void) myprint {
 nslog (@ "myprint called");

It has an instance method:myprint, we can call it after extension

Okay, with the above myclass, we will not add subclasses,How to add a helloworld method without modifying the myclass class?Just add two files, myclass + helloworld.h and myclass + helloworld.m.

Use "()" in the declaration file and implementation file to enclose the category name.The original class name + category "is the conventional file naming method.

See how these two files are implemented,Press command + n on xcoed to create a new file,Select the objective-c category method to create the class,This way xcode will automatically help you create files with a conventional naming scheme.

The category of category on is myclass.

In this way, xcode has created two files, myclass + helloworld.h and myclass + helloworld.m.

So now we add a helloworld method. Take a look at the implemented code as follows:

#import "myclass.h"
@interface myclass (helloworld)
-(void) helloworld;
#import "myclass + helloworld.h"
@implementation myclass (helloworld)
-(void) helloworld {
 nslog (@ "Hello London Olympics!");

Called in main

myclass * myclass=[[[myclass alloc] init] autorelease];
[myclass helloworld];
[myclass myprint];

Run and print the results:

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

What are the usage scenarios of category:

1, the class contains many method implementations,These methods require members of different teams to implement

2. When you are using classes in the base class library,When you don't want to inherit these classes and just want to add some methods.

category can fulfill the above requirements,Of course, there are issues that need attention when using category:

1, category can access the instance variables of the original class,But you cannot add instance variables,If i want to add variables,This is achieved by creating subclasses through inheritance.

2, category can overload the methods of the original class,Not highly recommended.This will override the methods of the original class.If you really want to reload,This is achieved by creating subclasses through inheritance.

3. What is different from ordinary interfaces is thatThe instance methods in the category's implementation file, as long as you don't call it, you don't need to implement all the methods declared.

  • Previous Android phone alarm clock usage example
  • Next aspnet paging function implementation