19th-day notes (Protocol-protocol) and IOS-Protocol

Source: Internet
Author: User

19th-day notes (Protocol-protocol) and IOS-Protocol

Knowledge points of IOS Learning (oc language)

1. Introduction to Protocols

1) concept: a protocol refers to an interface object negotiated between multiple objects. The Protocol provides some methods for the Protocol's implementers and proxies.
Communication

2) Declare the name of a protocol @ protocol <Base protocol> one protocol can comply with another protocol:
Base protocol (this Protocol has the declaration of all methods in the base protocol)

3) member variables cannot be declared in the protocol, but only methods can be declared.

4) declare a method in the Protocol. The default value is required, indicating that the method must be implemented. Otherwise, a system warning is reported, but no error is reported.

5) @ optional in the Protocol indicates that the method is optional (either implemented or not)

6) Protocol of use: A class complies with the Protocol, and the class name is later than the <protocol name>. The so-called protocol is: the class has the declaration of all methods in the protocol,
Methods defined in the Protocol must be implemented; a class can comply with multiple protocols (the class has methods declared in all protocols, and must implement all
Methods specified in the Agreement)

7) one agreement can comply with another agreement. After it complies with the agreement, it will have a declaration of all methods of the other agreement, and you can also add your own methods.

8) methods in which one protocol can comply with multiple protocols and possess all the base protocols

9) protocol declaration and usage example code:

1. Add a. h file named MyProtocol Based on Protocol to declare some Protocol methods such:

1 # import <Foundation/Foundation. h> 2 @ protocol MyProtocol <NSObject> 3 // member variables cannot be declared in the protocol, only method 4 // {5 // int _; 6 //} 7 // method declared in the Protocol. The default value is required 8-(void) test1; 9-(void) test2; 10 // @ required indicates that the method is required. 11 @ required12-(void) test3; 13 // @ optional indicates that the method is optional (either implemented or not implemented) 14 @ optional15-(void) test4; 16 @ end


2. Define a Person class to use the Protocol MyProtocol in the. h file. For example:

1 #import <Foundation/Foundation.h>2 #import "MyProtocol.h"   3 @interface Person : NSObject<MyProtocol>4 @property(nonatomic,copy)NSString *name;5 -(void)print;6 @end

 

3. Implement protocol methods in the. m file of the Person class, for example:

1 # import "Person. h "2 @ implementation Person 3-(void) print 4 {5 NSLog (@" person: % @ ", self. name); 6} 7 // Method 8-(void) test1 9 {10 NSLog (@ "test1"); 11} 12-(void) specified in the Implementation Protocol) test213 {14 15} 16-(void) test317 {18 19} 20 21 @ end

 

4. Execute the following methods in the main file:

1 Person * p1 = [[Person alloc] init]; 2 // determines whether the class to which p1 belongs complies with the protocol. 3 if ([p1 conformsToProtocol: @ protocol (MyProtocol)]) {4 // determine whether the class to which p1 belongs has implemented the Method 5 if ([p1 respondsToSelector: @ selector (test1)]) {6 [p1 test1]; 7} 8} 9 10 NSObject * obj1 = [[NSObject alloc] init]; 11 obj1 = nil; 12 NSObject * obj2 = @ "hello"; 13 obj2 = nil; 14 // set a limit of 15 NSObject <MyProtocol> * obj3 = @ "abc"; 16 // obj4 can point to any type of object, however, you must comply with the MyProtocol Protocol 17 id <MyProtocol> obj4 = [[Person alloc] init]; 18 Person <MyProtocol> * obj5 = [[Person alloc] init];


10) There is no multi-inheritance in OC, but it can be implemented through protocols. Multiple protocols can be used to implement methods similar to multi-inheritance.

11) protocols are mainly used to unify and standardize interfaces; methods for unified interfaces: inheritance and protocols

11) differences between protocols and inheritance

1. inheritance can also standardize interfaces: subclass inherits all methods of the parent class (you do not need to rewrite them unless the method of the parent class is not suitable for subclasses ),
The rewritten method name is the same as the parent class, which achieves the goal of unified interface. member variables can be added to subclass.
 
2. Protocol: If a class complies with the Protocol, the class has all the methods in the Protocol. If multiple classes comply with the same protocol, these classes have protocols.
To achieve the purpose of unified interfaces. The Protocol does not contain member variables.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.