IOS review Note 11: Protocol and proxy

Source: Internet
Author: User

IOS review Note 11: Protocol and proxy
1. Function
Methods can be declared in the Protocol (variables cannot be declared ),
As long as a class complies with this Protocol, it is equivalent to having all the method declarations in the Protocol.
The parent class is subject to the Protocol, and the subclass also has methods in the Protocol.
The protocol can also be bound to another protocol.
The Base protocol NSObject (also a base class), and The NSObject base class is also quasi-compliant with the base protocol.
The Base protocol contains common memory management methods: release and retain.


Ii. Definition
1 Protocol definition
@ Protocal protocol name
// Declaration method
@ End


2. Compliance with the agreement
2.1 Categories
@ Interface Class Name: parent class name <协议名1, 协议名2...>
@ End


2.2 Protocol
@ Protocal protocol name <协议名1, 协议名2...>
@ End


3 keywords
@ Required: Please implement it. The warning is false.
@ Optional (optional)


4. Define a variable that must abide by a certain protocol
NSObject <协议名> Obj1;
Id <协议名> Obj2;
@ Property (nonatomic, strong) id <协议名> P;


Sample Code

// MyProtocal. h @ protocal MyProtocal
 
  
-(Void) protocolDefaultFun (); @ required // default value, required for implementation. If not implemented, a warning will be reported-(void) protocolRequiredFun (); @ optional // optional-(void) protocolOptionalFun (); @ end
 

// MulProtocal.h@protocal MulProtocal 
 
  - (void)mulProtocolDefaultFun();@end
 

// Person. h # import
 
  
@ Protocal MyProtocal; @ protocal MulProtocal; // or @ protocal MyProtocal, MulProtocal; // If a class complies with a protocol, it owns all the methods declared in the Protocol @ interface Person: object
  
   
@ End
  
 


// Person.m#import "Person.h"#import "MyProtocal.h"#import "MulProtocal.h"@implementation Person 
 
  - (void)protocolDefaultFun(){}- (void)protocolRequiredFun(){}- (void)protocolOptionalFun(){}- (void)mulProtocolDefaultFun(){}@end
 


// Main. m # import
 
  
# Import "MyProtocal. h" # import "Person. h" int main () {// declare an object that complies with MyProtocal NSObject
  
   
* Obj1 = [[Person alloc] init]; obj1 = nil; id
   
    
Obj2 = [[Person alloc] init]; obj2 = nil; // return 0 ;}
   
  
 


Four proxies

// ticketDelegate.h@protocol ticketDelegate 
 
  - (int) leftTicketsNumber;@end
 

// Agent.h@protocol ticketDelegate;@interface Agent : NSObject 
 
  @end// Agent.m#import "Agent.h"#import "ticketDelegate.h"@implementation Agent- (int) leftTicketsNumber{return 1;}@end
 

// Person. h @ class Agent; @ class ticketDelegate; @ interface Person: NSObject-(void) buyTicket; // proxy property @ property (nonatomic, retain) id
 
  
Delegate; @ end
 

// Person.m#import "Person.h"#import "Agent.h"@implementation Person- (void)buyTicket{if ( _delegate & [_delegate leftTicketsNumber] > 0){NSLog(@"buy ticket");}}@end

// main.m#import "Person.h"#import "Agent"int main(){@autopool{Person* p = [[Person alloc]init];Agent* a = [[Agent alloc] init];p.delegate = a;[p buyTicket];}return 0;}




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.