Objective-c Protocol (Protocol), objectivecprotocol

Source: Internet
Author: User

Objective-c Protocol (Protocol), objectivecprotocol

The Protocol is a list of methods shared by multiple classes. methods listed in the Protocol are not implemented in this class, but are implemented by other classes. To comply with a protocol, a class must implement all methods of a specific protocol (except for optional methods ). A protocol is a list of methods. Any class can declare that it implements one or more protocols.

To define a protocol, you must use the @ protocol command followed by the protocol name. Then, you can declare some methods. The declaration of all methods before the instruction @ end is part of the protocol. The following code defines the NSCopying protocol on NSObject. h:

1    @protocol NSCopying2 3    - (id) copyWithZone:(NSZone *) zone;4 5    @end 

If your class decides to comply with the NSCopying protocol, you must implement the copyWithZone: method. By listing the protocol name in a pair of angle brackets in @ interface, the compiler is told that you are complying with a protocol. For example, the code @ interface Test: NSObject <NSObject> indicates: the parent class of the Test class is NSObject and complies with the NSCopying protocol.

To comply with multiple protocols, you only need to list multiple protocols in angle brackets and separate them with commas. For example, @ interface Test: NSObject <NSCopying, NSCoding>.

You can also define your own protocol, for example:

    

1 @ protocol Fly2 3-(void) go; 4-(void) stop; 5 @ optional // optional method, either implemented or not implemented 6-(void) sleep; 7 8 @ end

The following uses an example to describe the usage of the Protocol:

  

1 # import <Foundation/Foundation. h> 2 3 @ protocol Study 4 5-(void) study; 6-(void) stop; 7 8 @ optional // optional method, in this program, choose not to implement 9-(void) sleep; 10 11 @ end12 13 @ interface Test: NSObject <Study> {14 15} 16-(void) study; 17-(void) stop; 18 @ end19 20 @ implementation Test21 22-(void) study {23 NSLog (@ "good study, every day! "); 24} 25 26-(void) stop {27 NSLog (@" no learning, it will be difficult to succeed! "); 28} 29 @ end30 31 int main (int argc, const char * argv []) {32 @ autoreleasepool {33 Test * test = [Test new]; 34 35 [test study]; 36 [test stop]; 37 38 if ([test conformsToProtocol: @ protocol (Study)] = YES) // determine if the protocol is observed. 39 NSLog (@ "Study protocol is observed! "); 40 else41 NSLog (@" no agreement observed! "); 42} 43 return 0; 44}

At the same time, when creating a new protocol, it can also be expanded in the original protocol, for example, @ protocol Study1 <Study>.

The standard syntax defined in the Protocol is described as follows:

  

@ Optional indicates that classes that comply with the Protocol do not necessarily implement each method in the method declaration. @ required is a required method. The Protocol Class is similar to a public interface, which specifies interfaces between multiple classes.

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.