一.協議
1.定義:
OC或者iOS中的協議:一組方法聲明的列表,協議中不需要實現這些方法,誰遵守這個協議,誰就需要實現協議中的方法
2.協議創建的步驟
- 制定協議的內容(方法聲明列表)
- 遵守協議(.h)
- 實現協議中的方法(.m)
- 調用協議中的方法
3.聲明協議的關鍵字
(1)@required
表示必須實現的方法(默認)
(2)@optional
表示可以實現也可以不實現的方法
4.雙向協議
程序舉例
(1)ProtocolManToWoman.h
文件
#import <Foundation/Foundation.h>
@protocol ProtocolManToWoman <NSObject>//基協議
- (void)white;
- (void)rich;
- (void)beautiful;
@optional
- (void)writeCode;
@end
(2)ProtocolWomanToMan.h
文件
#import <Foundation/Foundation.h>
@protocol ProtocolWomanToMan <NSObject>
@required
- (void)height;
- (void)rich;
- (void)handsome;
@optional
- (void)writeCode;
@end
(3)Man.h
文件
#import <Foundation/Foundation.h>
#import "ProtocolToMan.h"
@interface Man : NSObject <ProtocolToMan>
- (void)height;
- (void)rich;
- (void)handsome;
//- (void)writeCode;
@end
(4)Man.m
文件
#import "Man.h"
@implementation Man
- (void)height{
NSLog(@"I am so high....");
}
- (void)rich{
NSLog(@"I am so rich....");
}
- (void)handsome{
NSLog(@"I am very handsome...");
}
//- (void)writeCode{
// NSLog(@"write hello world!....");
//}
@end
(5)Woman.h
文件
#import <Foundation/Foundation.h>
#import "ProtocolToWoman.h"
@interface Woman : NSObject <ProtocolToWoman>
- (void)white;
- (void)rich;
- (void)beautiful;
//- (void)writeCode;
@end
(6)Woman.m
文件
#import "Woman.h"
@implementation Woman
- (void)white{
NSLog(@"white.....");
}
- (void)rich{
NSLog(@"rich.....");
}
- (void)beautiful{
NSLog(@"beautiful......");
}
//- (void)writeCode{
// NSLog(@"writting code....");
//}
@end
(7)main
文件
#import <Foundation/Foundation.h>
#import "Man.h"
#import "Woman.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Man *man = [[Man alloc]init];
if ([man respondsToSelector:@selector(height)]) {
[man height];
}
if ([man respondsToSelector:@selector(rich)]) {
[man rich];
}
if ([man respondsToSelector:@selector(handsome)]) {
[man handsome];
}
if ([man respondsToSelector:@selector(writeCode)]) {
[man writeCode];
}
else{
NSLog(@"can not write code");
}
Woman *woman = [[Woman alloc]init];
if ([woman respondsToSelector:@selector(white)]) {
[woman white];
}
if ([woman respondsToSelector:@selector(rich)]) {
[woman rich];
}
if ([woman respondsToSelector:@selector(beautiful)]) {
[woman beautiful];
}
if ([woman respondsToSelector:@selector(writeCode)]) {
[woman writeCode];
}
}
return 0;
}
二.委託與代理
委託代理與正向傳值程序舉例
(1)ProtocolBossToWorker.h
文件
#import <Foundation/Foundation.h>
@protocol ProtocolBossToWorker <NSObject>
- (void)driveCar:(int)time;
@end
(2)ProtocolWorkerToBoss
文件
#import <Foundation/Foundation.h>
@protocol ProtocolWorkerToBoss <NSObject>
- (void)payMoney:(int)money;
@end
(3)Boss.h
文件
#import <Foundation/Foundation.h>
#import "ProtocolBossToWorker.h"
#import "ProtocolWorkerToBoss.h"
@interface Boss : NSObject<ProtocolWorkerToBoss>
@property (retain,nonatomic)id <ProtocolBossToWorker>agent;
@property (assign,nonatomic)int timeOfDrive;
- (void)payMoney:(int)money;
- (void)getDrive;
@end
(4)Boss.m
文件
#import "Boss.h"
@implementation Boss
- (void)payMoney:(int)money{
NSLog(@"boss pay the money %d",money);
}
- (void)getDrive{
if ([self.agent respondsToSelector:@selector(driveCar:)]) {
[self.agent driveCar:_timeOfDrive];
}
}
@end
(5)Worker.h
文件
#import <Foundation/Foundation.h>
#import "ProtocolBossToWorker.h"
#import "ProtocolWorkerToBoss.h"
@interface Worker : NSObject<ProtocolBossToWorker>
@property (retain,nonatomic)id <ProtocolWorkerToBoss>agent;
@property (assign,nonatomic)int money;
- (void)driveCar:(int)time;
- (void)getPay;
@end
(6)Worker.m
文件
#import "Worker.h"
@implementation Worker
- (void)driveCar:(int)time{
NSLog(@"drive for %d hours",time);
_money = time*5;
}
- (void)getPay{
if ([self.agent respondsToSelector:@selector(payMoney:)]) {
[self.agent payMoney:_money];
// [self.agent payMoney:[self driveCar:self.boss.timeOfDrive]];
}
}
@end
(7)main.m
文件
#import <Foundation/Foundation.h>
#import "Boss.h"
#import "Worker.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Boss *boss = [[Boss alloc]init];
Worker *worker = [[Worker alloc]init];
boss.agent = worker;
boss.timeOfDrive = 100;
worker.agent = boss;
[boss getDrive];
[worker getPay];
}
return 0;
}