OC-Concepts and usage of protocols, oc-concepts of protocols
In the previous article, we introduced the class extension in OC: Extend.
The protocol in OC is equivalent to the interface (abstract class) in Java, but the name in OC is more vivid, because when we are learning interfaces in Java, it can be seen that the interface is equivalent to a contract (Protocol), marking its implementation class. Of course, this is replaced by annotations after Java5.0, this is because the annotation was created for this function.
The Protocol defines a set of methods and allows other classes to implement them.
Let's look at the code below:
WithProtocol. h
//// WithProtocol. h // 11_ProtocolDemo /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> @ protocol WithProtocol <NSObject> // It is required by default. // it must be implemented @ required-(void) finshTask;-(void) dontLate; // optional implementation @ optional-(void) wearNeat; @ end
Here we define a protocol WithProtocl.
Protocol definition format:
@ Protocol name <parent protocol>
Definition Method
@ End
Note: The keyword defining the protocol is @ protocol, and the protocol can inherit the parent protocol.
The methods defined in the Protocol also have two modifiers:
@ Required: This indicates that this method must be implemented by other classes and is also the default value.
@ Optional: This indicates that this method is optional for other class implementations.
This is similar to the abstract class in Java. If it is an abstract modifier, it must be implemented. Therefore, if there is no @ optional modifier in a protocol, this Protocol is equivalent to the interface in Java.
Note that in the above Code, NSObject is not the NSObject class we mentioned earlier, but the NSObject protocol. It is also the first protocol in OC, it does not matter if the name is the same in OC.
Let's take a look at the protocol usage:
Student. h
//// Student. h // 11_ProtocolDemo /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> # import "WithProtocol. h "@ interface Student: NSObject <WithProtocol>-(void) study; @ end
The Protocol is simple,
Directly after the inheritance class (NSObject) <protocol name>
Student. m
//// Student. m // 11_ProtocolDemo /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import "Student. h "@ implementation Student-(void) study {NSLog (@" study ");} // directly in. m file. re-define # pragma mark-WithProtocol-(void) finshTask {NSLog (@ "");}-(void) in the H file) dontLate {// # The warning code is supplemented with NSLog (@ "not late") in a few days;}-(void) wearNeat {NSLog (@ "dressed neatly");} @ end
Then, in the Implementation class, we need to implement the methods required in the Protocol.
Note:
# Pragma mark-WithProtocol
This function is to mark the methods that follow the mark as methods in the Protocol. In this way, you can classify the methods in a class into more details. We can view them in the file navigation bar:
Place the cursor in # param, and the above file bar will show @ implementation Student
Then, click @ implementation Student.
We can see that the methods in the Protocol and the methods of the class are separated, which facilitates browsing.
Another one is
# Adding the warning code in a few days
This is to mark the code here with a warning. Xcode will display a yellow mark here. This is to add a flag for yourself and check it later.
For example, during the development process, the code here still has some problems, but it may not be able to be processed temporarily. If you have time to modify it later, you just need to mark it.
Test class:
//// Main. m // 11_ProtocolDemo /// Created by jiangwei on 14-10-11. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> # import "Student. h "int main (int argc, const char * argv []) {@ autoreleasepool {Student * stu = [[Student alloc] init]; [stu finshTask]; [stu dontLate]; // determines whether the wearNeat method has implemented if ([stu respondsToSelector: @ selector (wearNeat)]) {[stu wearNeat];} return 0 ;}
Here is a method
RespondsToSelector: @ selectorThe function of this method is to determine whether a method is defined in the current object. This method is still very useful. If it is in Java, we may need to implement it using reflection.
Summary
The Protocol is also a very important concept in OC. The protocol is used in many places in the Foundation framework. In fact, it is very similar to the abstract class and interface in Java.