Use category.When the source code of a class cannot be obtained and a method needs to be added to the class, the function of the class is very important.
The syntax for creating a category is also very simple. The Code is as follows:
@interface Student (Test)-(void)test;@end
@ Implementation Student (Test)-(void) test {NSLog (@ "here is the test method of the Student Class");} @ end
The declaration and implementation keywords are the same as before.
The difference is that you do not need to specify the parent class during the declaration, but directly write the name of the class to add the method. However, you must add a bracket and write the category name in the brackets. A class can have multiple categories.
With this category, although the source code of the Student class does not contain the test method, the Student object can still be used to call this method normally.
Student * s = [[Student alloc] init];[s test];
2. ExtendExtension can be seen as an anonymous category. Classes sometimes need to be seen only by themselves. Private methods used can be declared through extension.
For example, the Student class in the previous section has a cheating method, which is not viewed by the outside, but does not affect the calls. This method can be declared in the extension, and the implementation of the method is still in the Student class.
@interface Student ()-(void)zuobi;@end
Generally, we place the class declaration in a file with the extension. h and the implementation in a file with the extension. m. The. m file of the executable package type in production will be compiled and encrypted into the. a file and irreversible.
Student. h file
@interface Student : NSObject@property(nonatomic,strong)NSString * name;@end
Student. m file
@ Interface Student ()-(void) zuobi; @ end @ implementation Student (Test)-(void) test {NSLog (@ "here is the test method for the Student class ");} -(void) zuobi {NSLog (@ "cheating");} @ end
Generally, the extended declaration is stored in the. m file, so that the class method can be hidden and not discovered by the outside world and can be used normally in the class.
3. ProtocolThe protocol in OC is a declaration of a set of methods. It is not required. The compliance class is responsible for implementing the methods in the Protocol.
Protocol usage@protocol
Keyword.
The declared methods include required methods and optional methods. Use@required
And@optional
Keyword.
If this parameter is left blank, the default method is required.
@ Protocol Test
@ Required-(void) fun1; // class that complies with this Protocol, required method @ optional-(void) fun2; // class that complies with this Protocol, optional implementation method @ end
Making an agreement is simple. We can define the behavior of an object through the agreement.
For example, the following method
-(void)test:(id
)obj;
This method requires,obj
The object must be an object created by a class that complies with the Test protocol.
@property id
obj;
Likewise, attribute objects can be defined using protocols.