1 Function description
1.1 Add a method to a class that already exists, and you cannot add a member variable.
1.2 Putting the implementation of a class into a different file
1.3 Creating a reference to a private method
2 format
Header file (file name: "Class name + category name. h")
#import "class name. h"
@interface class name (category name)
Declaration of the new method
@end
source file (file name: "Class name + category name. M")
#import "class name + category name. h"
@interface class name (category name)
New approach to implementation
@end
3 Example
Suppose you now have a person class, as follows:
Person.h#import <Foundation/Foundation.h> @interface Person:nsobject@end
Person.m
#import "Person.h" @implementation person@end
Now we want to add a eat and run method to the person class:
Person+action.h
#import "Person.h" @interface person (action)-(void) eat;-(void) run; @end
Person+action.m
#import "Person+action.h" @implementation person (action)-(void) eat{}-(void) run{}
Thus the person class has the eat and run methods, all of which can be called by two methods.
iOS Review note 12: Categories