iOS 协议

  • 协议
    只做方法声明。本质是一组方法列表,用来规范接口。

  • 应用,适用场景
    用对象本身体现客观实体,用类对对象进行描述,通过让类遵守相应协议,根据具体业务逻辑进行对应实现来规范对象对行为。协议的遵守者一定是类本身。按照协议规定的方法执行。

创建,制定协议

//
//  PersonProtocol.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol PersonProtocol <NSObject>

@required
- (void)eat;

@optional
- (void)work;
- (void)play;

@end
//
//  PoliceProtocol.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol PoliceProtocol <NSObject>

- (void)catchThief;

@end
//
//  ThiefProtocol.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol ThiefProtocol <NSObject>

- (void)steal;

@end

遵守

//
//  Person.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

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

@interface Person : NSObject<PersonProtocol>

@end
//
//  Person.m
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Person.h"

@implementation Person

- (void)eat {
    NSLog(@"eat");
}

@end

//
//  Police.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Person.h"
#import "PoliceProtocol.h"

@interface Police : Person<PoliceProtocol>

@end
//
//  Police.m
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Police.h"

@implementation Police

- (void)eat {
    NSLog(@"eat");
}

- (void)work {
    NSLog(@"抓小偷");
}

- (void)catchThief {
    [self work];
}

@end

//
//  Thief.h
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Person.h"
#import "ThiefProtocol.h"

@interface Thief : Person<ThiefProtocol>

@end
//
//  Thief.m
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Thief.h"

@implementation Thief

- (void)steal {
    NSLog(@"steal");
}

@end

使用

//
//  ViewController.m
//  LearnProtocol
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"
#import "Police.h"
#import "Thief.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    Person *person = [[Person alloc] init];
    [person eat];
    //[person work];//未实现,调用崩溃
    
    Police *police = [[Police alloc] init];
    [police catchThief];
    
    Thief *thief = [[Thief alloc] init];
    [thief steal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

<NSObject>是一个协议,不是类。是父协议。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS项目中代理是一种不可缺少的消息传递方式,代理主要由三部分组成: 协议:用来规定代理双方可做什么,必须做什么...
    Jalynn葸阅读 2,089评论 0 3
  • 最近看了许多有关协议与代理的文章,自己试着总结了一下,如果有什么不对的地方欢迎指出~ delegate和 prot...
    噫那里有条咸鱼阅读 1,520评论 4 11
  • 以下是我总结的协议传值封装的方法。 问题:如果A页面跳转B,B需要回传值至A页面,怎么回传? 当然,回传有很多方式...
    handsome丶亮阅读 1,099评论 3 4
  • 上次写了窗口的相关内容,突然觉得很low。low到我自己看到就起鸡皮疙瘩,我想对于但凡对于编程有些了解的人都...
    磊CC阅读 943评论 1 2
  • 协议: OC中的协议类似于Java中的接口,是一个功能方法的集合,但协议本身不是一个类不会自己去实现协议里的方法,...
    iOS_肖晨阅读 737评论 0 50

友情链接更多精彩内容