您的位置:首页 > 移动开发 > IOS开发

自学iOS开发系列----OC(协议和委托代理)

2016-12-05 21:00 411 查看
协议

1.协议是OC的一种语法。OC的协议,类似于JAVA中接口,用来规范函数声明,实现多继承或对象间通信的一种语法。

2.协议的两种写法

①command+N新建协议类(HTProtocol)



②为协议命名



③HTProtocol.h

//第一种写法
#import <Foundation/Foundation.h>

@protocol HTProtocol <NSObject>

- (void)sayHello;

@end


④新建Person类

1)Person.h

#import <Foundation/Foundation.h>
#import "HTProtocol.h"

//第二种写法,最常用的
@protocol PersonDelegate <NSObject>

- (void)run;

@end
//遵守多个协议
@interface Person : NSObject <
HTProtocol,
PersonDelegate
>

@end


2)Person.m

#import "Person.h"

@implementation Person

- (void)sayHello {
NSLog(@"世界,你好");
}

- (void)run {
NSLog(@"所有的人都在跑步");
}

@end


⑤main.m

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {

Person * person = [[Person alloc] init];
[person sayHello];
[person run];

}
return 0;
}


3.协议中的关键字

@required:当某个类遵守了此协议,必须要实现的方法,默认属性;

@optional:当某个类遵守了此协议,可以不实现,日常工作中最常用;

默认声明的协议为@required关键字,必须实现的协议未实现,则程序运行报错。

实战应用:

①新建协议类WorkProtocol

WorkProtocol.h

#import <Foundation/Foundation.h>

@protocol WorkProtocol <NSObject>

//默认为@required
- (void)read;

//当某个类遵守了此协议,必须要实现的方法,默认属性
@required
- (void)writeCode;

//当某个类遵守了此协议,可以不实现,日常工作中最常用
@optional
- (void)writeDocuments;

@end


②新建Worker类

1)Worker.h

#import <Foundation/Foundation.h>
#import "WorkProtocol.h"

@interface Worker : NSObject<
WorkProtocol>

@end


2)Worker.m

#import "Worker.h"

@implementation Worker

- (void)read {
NSLog(@"Worker read");
}

- (void)writeCode {
NSLog(@"Worker writeCode");
}

- (void)writeDocuments {
NSLog(@"Worker writeDocuments");
}

@end


③main.m

#import <Foundation/Foundation.h>
#import "Worker.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {

Worker * worker = [[Worker alloc] init];
[worker read];
[worker writeCode];
[worker writeDocuments];

}
return 0;
}


4.OC中只有单继承,没有多继承,但是可以通过协议的方式实现多继承

①新建ClassA类

1)ClassA.h

#import <Foundation/Foundation.h>

@protocol ClassAProtocol <NSObject>

- (void)classAMethod;

@end

@interface ClassA : NSObject <ClassAProtocol>

@end


2)ClassA.m

#import "ClassA.h"

@implementation ClassA

- (void)classAMethod {
NSLog(@"class A Method");
}

@end


②新建协议类ClassBProtocol

ClassBProtocol.h

#import <Foundation/Foundation.h>

@protocol ClassBProtocol <NSObject>

- (void)classBMethod;

@end


③新建ClassC类,模拟多继承

1)ClassC.h

#import <Foundation/Foundation.h>
#import "ClassA.h"
#import "ClassBProtocol.h"

@interface ClassC : NSObject <
ClassAProtocol,
ClassBProtocol>

@end


2)ClassC.m

#import "ClassC.h"

@implementation ClassC

- (void)classAMethod {
NSLog(@"ClassA");
}

- (void)classBMethod {
NSLog(@"ClassB");
}

@end


④main.m

#import <Foundation/Foundation.h>
#import "ClassC
f034
.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {

ClassC * clsC = [[ClassC alloc] init];

[clsC classAMethod];
[clsC classBMethod];

}
return 0;
}


委托代理

1.委托代理就好比现实生活中,我们如果想做成一件事,有的时候仅靠我们自己是无法达到目的的,需要通过他人的帮助才能完成。

2.通过类的复合理解委托代理的含义

如下情景:小明去找房子,通过中介,中介找到房子后,把信息会告诉你,你看看房子是否满意,如果满意租下来,如果不满意,让中介继续找。

①新建Agent类

1)Agent.h

#import <Foundation/Foundation.h>

typedef enum : NSInteger{
HighHouseType,    //高档房
MiddleHouseType,  //普通房
LowHouseType      //廉价房
}HouseType;

@interface Agent : NSObject

@property (nonatomic, assign) HouseType type; //记录房子的信息

- (HouseType)findHouse;

@end


2)Agent.m

#import "Agent.h"

@implementation Agent

- (HouseType)findHouse {
self.type = arc4random() % 3;

switch (self.type) {
case HighHouseType:{
NSLog(@"找到了一个高档房子");
break;
}
case MiddleHouseType:{
NSLog(@"找到一个价格合适的普通房子");
break;
}
case LowHouseType:{
NSLog(@"找个一个廉价的房子");
break;
}
default:
break;
}

return self.type;
}

@end


②新建Person类

1)Person.h

#import <Foundation/Foundation.h>
#import "Agent.h"

@interface Person : NSObject

- (BOOL)wantToFindHouseWithAgent:(Agent *)agent;

@end


2)Person.m

#import "Person.h"

@implementation Person

- (BOOL)wantToFindHouseWithAgent:(Agent *)agent {
HouseType type = [agent findHouse];

switch (type) {
case HighHouseType:{
NSLog(@"房子不错,就是太贵了");
break;
}

case MiddleHouseType:{
NSLog(@"这个房子最好了,就是我喜欢的类型,价格也能接受");
break;
}

case LowHouseType:{
NSLog(@"房子太破了");
break;
}
default:
break;
}

if (type == MiddleHouseType) {
return YES;
}
return NO;
}

@end


③main.m

#import <Foundation/Foundation.h>
#import "Agent.h"
#import "Person.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {

Person * xiaoming = [[Person alloc] init];
Agent * xiaoli = [[Agent alloc] init];

while (1) {
sleep(1);
BOOL success = [xiaoming wantToFindHouseWithAgent:xiaoli];
if (success) {
break;
}
}

NSLog(@"终于找到最合适的房子了,好开森!");
}
return 0;
}


3.通过委托代理实现上面的情景

①新建Agent类

1)Agent.h

#import <Foundation/Foundation.h>

typedef enum : NSInteger{
HighHouseType,
MiddleHouseType,
LowHouseType
}HouseType;

//声明协议
@protocol FindHouseProtocol <NSObject>

- (void)looklookOK:(int)type location:(NSString *)location;

@end

@interface Agent : NSObject

//声明代理
@property (nonatomic, assign) id<FindHouseProtocol>delegate;

@property (nonatomic, assign) HouseType type;

- (void)startFindHouse;

@end


2)Agent.m

#import "Agent.h"

@implementation Agent

- (void)startFindHouse {
self.type = arc4random() % 3;

switch (self.type) {
case HighHouseType:{
NSLog(@"找到了一个高档房子");
break;
}
case MiddleHouseType:{
NSLog(@"找到一个价格合适的普通房子");
break;
}
case LowHouseType:{
NSLog(@"找个一个廉价的房子");
break;
}
default:
break;
}

NSArray * array = @[@"昌平",@"海淀",@"朝阳"];

//谁刚才让我去找房子 我找到之后就让谁来看房子
[self.delegate looklookOK:self.type location:array[arc4random()%3]];
}

@end


②新建Person类

1)Person.h

#import <Foundation/Foundation.h>
#import "Agent.h"

@interface Person : NSObject<FindHouseProtocol> {
BOOL _OK;
}

- (void)findHouse;

@end


2)Person.m

#import "Person.h"

@implementation Person

- (void)findHouse {
Agent * agent = [[Agent alloc] init];

agent.delegate = self;

_OK = NO;

while (1) {
sleep(1);
[agent startFindHouse];
if (_OK) {
break;
}
}
}

- (void)looklookOK:(int)type location:(NSString *)location {
switch (type) {
case HighHouseType:{
NSLog(@"房子不错,就是太贵了");
break;
}

case MiddleHouseType:{
NSLog(@"这个房子最好了,就是我喜欢的类型,价格也能接受");
break;
}

case LowHouseType:{
NSLog(@"房子太破了");
break;
}
default:
break;
}

if (type == MiddleHouseType && [location isEqualToString:@"昌平"]) {
_OK = YES;
}
}

@end


③main.m

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {

Person * xiaoming = [[Person alloc] init];
[xiaoming findHouse];

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息