IOS review notes 12: categories, ios review notes 12
1. Function Description
1.1 add methods to existing classes. member variables cannot be added.
1.2 place the implementation of classes in different files
1.3 create a reference to a private Method
2 format
Header file (file name: "class name + class name. h ")
# Import "class name. h"
@ Interface Class Name (category name)
// Declaration of the new method
@ End
Source file (file name: "class name + class name. m ")
# Import "class name + class name. h"
@ Interface Class Name (category name)
// New Implementation Method
@ End
3 Examples
Assume that there is a Person class, as shown below:
// Person. h # import <Foundation/Foundation. h> @ interface Person: NSObject @ end
// Person. m
# Import "Person. h" @ implementation Person @ end
Now we want to add an 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 {}
In this way, the Person class has the eat and run methods, and all of them can call two methods.