For every object that can have a delegate, there is a corresponding protocol that declares the
messages that the object can send its delegate. The delegate implements methods from the protocol for
events it is interested in. When a class implements methods from a protocol, it is said to conform to
the protocol.

Note that a protocol is not a class; it is simply a list of method declarations. You cannot create
instances of a protocol, it cannot have instance variables, and these methods are not implemented
anywhere in the protocol. Instead, implementation is left to each class that conforms to the protocol.

Before sending an optional message, the object first asks its delegate if it is okay to send that message
by sending another message, respondsToSelector:. Every object implements this method, which
checks at runtime whether an object implements a given method.

If a method in a protocol is required, then the message will be sent without checking first. This means
that if the delegate does not implement that method, an unrecognized selector exception will be
thrown, and the application will crash.

To prevent this from happening, the compiler will insist that a class implement the required methods
in a protocol. But for the compiler to know to check for implementations of a protocol’s required
methods, the class must explicitly state that it conforms to a protocol. This is done either in the class
header file or the class extension: the protocols that a class conforms to are added to a commadelimited
list inside angled brackets in the interface declaration.

protocol的更多相关文章

  1. 学习笔记:URL Protocol在浏览器中打开本地应用程序

    看到阿里的网站上可以通过点击卖家的旺旺图标从而调用本地的阿里旺旺程序,而且还可以传递当前浏览者需要咨询的商品.这是怎么实现的呢?是通过URLProtocol来完成. 原理还没有太清楚,即在系统里注册一 ...

  2. Protocol Buffer搭建及示例

    本文来源:http://www.tanhao.me/code/150911.html/ Protocol Buffer(简称Protobuf或PB)是由Google推出的一种数据交换格式,与传统的XM ...

  3. 从零开始山寨Caffe·伍:Protocol Buffer简易指南

    你为Class外访问private对象而苦恼嘛?你为设计序列化格式而头疼嘛? ——欢迎体验Google Protocol Buffer 面向对象之封装性 历史遗留问题 面向对象中最矛盾的一个特性,就是 ...

  4. oracle plsql 无法连接 报 ORA-12560: TNS:protocol adapter error

    ORA-12560: TNS:protocol adapter error 添加一个环境变量,名为TNS_ADMIN,值为 tnsnames.ora 文件所在路径.比如我的本机为:D:/instant ...

  5. 窥探Swift之协议(Protocol)和委托代理(Delegate)回调的使用

    协议与委托代理回调在之前的博客中也是经常提到和用到的在<Objective-C中的委托(代理)模式>和<iOS开发之窥探UICollectionViewController(四) - ...

  6. ORA-12516:TNS:listener could not find available handler with matching protocol stack

    应用程序连接测试数据库时报ORA-12516:TNS:listener could not find available handler with matching protocol stack 检查 ...

  7. Serial Communication Protocol Design Hints And Reference

    前面转载的几篇文章详细介绍了UART.RS-232和RS-485的相关内容,可以知道,串口通信的双方在硬件层面需要约定如波特率.数据位.校验位和停止位等属性,才可以正常收发数据.实际项目中使用串口通信 ...

  8. [原创翻译]Protocol Buffer Basics: C#

    Protocol Buffer 基础知识:c#    原文地址:https://developers.google.com/protocol-buffers/docs/csharptutorial   ...

  9. 让Web API支持Protocol Buffers

    简介 现在我们Web API项目基本上都是使用的Json作为通信的格式,随着移动互联网的兴起,Web API不仅其他系统可以使用,手机端也可以使用,但是手机端也有相对特殊的地方,网络通信除了wifi, ...

  10. Xml,Json,Hessian,Protocol Buffers序列化对比

    简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...

随机推荐

  1. 3.Complementing a Strand of DNA

    Problem In DNA strings, symbols 'A' and 'T' are complements of each other, as are 'C' and 'G'. The r ...

  2. iOS复选框

    这种按钮iOS没有原生效果. 可以靠按钮的不同点击状态来实现这个效果. 代码如下: _workBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [ ...

  3. ArcGIS Earth

    恩,万众瞩目的ArcGIS Earth,现在华丽丽的可以在官网上下载了 满怀希望的心花怒放的我就去下载了...... 然后得然后...... 打开界面简洁的不要不要的,连个Esri的logo都没有.好 ...

  4. codeforces B.Fixed Points

    link:http://codeforces.com/contest/347/problem/B 很简单,最多只能交换一次,也就是说,最多会增加两个.可能会增加一个.也可能一个也不增加(此时都是fix ...

  5. Xcode 6制作动态及静态Framework

    技术交流新QQ群:414971585 有没有写SDK或者要将一些常用的工具类做成Framework的经历? 你或许自己写脚本完成了这项工作,相信也有很多的人使用 iOS-Universal-Frame ...

  6. MongoDB windows解压缩版安装

    创建目录如下 将mongodb的压缩包解压到mongodb目录下 mongodata下创建data目录存放数据:创建log目录存放目录 配置服务,cmd 输入命令: D:\mongo\mongodb\ ...

  7. Ubuntu配置VNC server

    安装vncserver后,默认的配置下只有一个很"朴素"的图形界面(没有抓图,就一个黑白窗口),要支持Ubuntu的桌面,并且支持和windows之前复制粘贴文字,需要修改xsta ...

  8. 关于 一开始不懂得 hosts配置。

    是转载别人的. 原文: http://my.oschina.net/cxz001/blog/298228   感谢分享: 一开始抄着陪着win下的hosts文件.然后配置  apache中的hosts ...

  9. LeetCode----Tree

    Path Sum II 思路:回溯 public List<List<Integer>> pathSum(TreeNode root, int sum) { List<L ...

  10. [转]权限问题导致Nginx 403 Forbidden错误的解决方法

    权限问题导致Nginx 403 Forbidden错误的解决方法 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-08-22 这篇文章主要介绍了权限问题导致Nginx 403 F ...