IOS Protocol Protocol

Source: Internet
Author: User

The protocol in iOS is the declaration of a method, stating that no implementation is required, and that the part of the implementation is done by the compliance party.

Look at a small example of a protocol: this example is to give two protocols, and then create two types of people to abide by the agreement.

Rogue Protocol (ROGUEDELEGATE.H)

 #import<Foundation/Foundation.h>/** Rogue Protocol*/  @protocolRoguedelegate <NSObject>@required//methods that must be implemented  /** Eat*/- (void) eat; /** Cheat*/- (void) lie; @optional//optional methods of implementation- (void) drink; @end

Gentleman's agreement (GentleDelegate.h)

  /* * Gentleman Agreement */  @protocol Gentledelegate <NSObject>  //  protocol content only methods cannot have attributes   //  protocol methods  /* * Mouth No hands */  -(void) fight;   /* * Love Money take the proper way */  -(void) Lovemoney;   @end

The object of compliance with the agreement

  #import<Foundation/Foundation.h>#import "RogueDelegate.h"  #import "GentleDelegate.h"  //the class that adheres to the agreement is the proxy class; If a class complies with the Protocol, it is necessary to implement the Protocol method;  /** Comply with the Rogue protocol agent*/  //A class can comply with multiple protocols at the same time, with multiple protocol names separated by commas in a <>  @interfaceYhperson:nsobject <roguedelegate, gentledelegate>@end  @interfaceYhstudent:nsobject <RogueDelegate>-(void) study; @end

Implementation protocol

  #import "YHPerson.h"  @implementationYhperson- (void) Eat {NSLog (@"eating a King's meal !"); }  - (void) Lie {NSLog (@"It 's a lie !"); }  - (void) Fight {NSLog (@"mouth no Hands !"); }  - (void) Lovemoney {NSLog (@"Love money to take the proper way!"); }  @end  @implementationyhstudent//implementing student-related methods- (void) Study {NSLog (@"good good Study, day."); }  //ways to implement rogue protocols- (void) Eat {NSLog (@"after eating with Rob"); }  - (void) Lie {NSLog (@"All Mercy!! "); }  @end

Concepts in the Protocol

Delegate: A party entrusted with the appointment of a person, a party to the agreement

Agent: The party accepting the entrustment; the party that implements the agreement.

Protocol: An agent's code of conduct, that is, a statement that the agent needs to implement the method

Features of the Protocol: 1. A class can comply with multiple protocols (as if a company can serve a number of companies)

       2. The same agreement can be adhered to by multiple classes (as if a company can serve multiple companies)  

The way a class adheres to a protocol, it is necessary to implement the method stipulated in this agreement.

Here are the specific examples: The boss wants to do an iOS project, but he won't do it, find someone who will develop iOS to help him

Declare the protocol in YHBoss.h, declare the delegate attribute, and implement the principal-agent method

  #import<Foundation/Foundation.h>//2. Agreement  /** Declaration Agreement*/  @protocolStaffdelegate <NSObject>/** will iOS development (do development)*/- (void) Codeios; @end  //1. Delegation//the delegate needs a compliance object to help him do things (delegates need a proxy)  /** Delegate Class (make agreement)*/  @interfaceYhboss:nsobject//any object created by a class that adheres to the Agreement can act as an agent for the boss//Note: Delegate to be decorated with weak (prevents the object from being destroyed by circular references)  /** Agent*/@property (nonatomic, weak)ID<StaffDelegate>Delegate; //Delegate Agent Method (tell the agent when to do things)  /** Tell the agent*/- (void) askstaffworking; @end

Implement the principal-agent method in the YHBOSS.M file

  #import " YHBoss.h "  @implementation Yhboss   // entrusted to tell the agent to do things  -(void) askstaffworking {       NSLog (@ " boss tells employees to start ")       ;  //  agent to start doing things      [self.  Delegate  Codeios];  }   @end

Compliance with agreements in employee YHPerson.h documents

  #import <Foundation/Foundation.h>  #import"YHBoss.h"  // 3. Agent (complying with the Agreement, implementing the Protocol method)  /* * Agent */  @interface Yhperson:nsobject <StaffDelegate>  @end

Implement proxy methods in employee YHPERSON.M files

 #import "YHPerson.h"  #import "YHBoss.h"  @implementationYhperson//determine if an object implements a method//(This determines whether self.delegate implements the Buyphone method)  if([Self.Delegaterespondstoselector: @selector (Codeios)]) {      //agent starts to do things[Self.DelegateCodeios]; }  @end

Create a Boss object and an employee object in the main file to let the employee object become the agent of the boss when the boss asks the employee to work, the employee realizes the method in the agreement.

  #import<Foundation/Foundation.h>#import "YHPerson.h"  intMainintargcConst Char*argv[]) {@autoreleasepool {//1. First you need a bossYhboss *boss =[[Yhboss alloc] init]; //2. Need an employeeYhperson *person =[[Yhperson alloc] init]; //3. Make employees an agent of the bossBoss.Delegate=Person ; //4. The boss shouted to start[boss Askstaffworking]; }      return 0; }

The role of the protocol agent in real-world development: pass-through (reverse) and callback. In the next article.

IOS Protocol Protocol

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.