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. css background-size

    先来看下语法:background-size: length|percentage|cover|contain;具体的值,百分比都ok,w3c上面说的很清楚,当时具体的值或者百分比的时候,第一个表示宽 ...

  2. 如何通过IP地址添加网络打印机

    以惠普的HP LaserJet 8100 Series PCL6为例. 1.从开始菜单选择“打印机和传真”. 2.鼠标右键“添加打印机”,然后单击“下一步”,选择“连接到这台计算机的本地打印机”,注意 ...

  3. ajax处理的方式

    1.方式一:在一个web页面中 [System.Web.Services.WebMethod],有点象webservers的方法! 前台: 返回的数据: msg:Object {d: "He ...

  4. BZOJ 2456 杂题 卡内存

    2456: mode Time Limit: 1 Sec  Memory Limit: 1 MBSubmit: 3702  Solved: 1551[Submit][Status][Discuss] ...

  5. Ajax在html页面获取后台XML文件资源

    一.准备工具 站长吧ASP调试工具.exe,这个工具是为了快速建立asp环境,方便调试. 二.建立文件夹 1.建立网站根文件夹,名字随意,将站长吧ASP调试工具.exe复制到根文件夹: 2.建立xml ...

  6. 博客开篇:随笔《从windows到linux的转变》。

    在QQ群里讨论到了WINDOWS和LINUX.MAC,用手机码了如下回复,索性转过来当做博客的开篇.:) unix 和linux 在外很火的主要原因是开源,国外崇尚自由的精神是从出生就在细胞里的,而w ...

  7. JSBinding+Bridge.Net:框架代码与逻辑代码的关系

    在JSB+Bridge工程中你可以同时维护Cs版本和Js版本的游戏. 框架代码:简称framework,表示那些不进行热更的代码.注意,这包括你自己写的代码,也包括引用的Dll,比如UnityEngi ...

  8. ThinkPHP中的动态缓存(S方法)和快速缓存(F方法)

    系统默认的缓存方式是采用File方式缓存,我们可以在项目配置文件里面定义其他的缓存方式,例如,修改默认的缓存方式为Xcache(当然,你的环境需要支持Xcache)    对于File方式缓存下的缓存 ...

  9. 如何在IDEA 中使用Git

    1,下载最新的 git 包 地址: https://git-scm.com/download/win 下载便携版 64,32 根据个人爱好   2,解压后随便放个位置即可,例如图: (不太建议使用它自 ...

  10. 处理大并发之五 使用libevent利器bufferevent

    转自:http://blog.csdn.net/feitianxuxue/article/details/9386843 处理大并发之五 使用libevent利器bufferevent 首先来翻译一段 ...