一、协议
协议主要是提供接口,在iOS中模拟多继承,他的核心意义在——约定!
优点:
- 实现简单,容易理解
- 强类型检查
缺点:
- 建立了比较强的耦合关系
- 有可能需要较长期保存委托以进行回调。如果保留的委托需要有独占性,可能会给单件模式、以及多线程带来麻烦
- 类只能通过一个方法完成一种类型的回调。代码逻辑很容易集中到一个方法中。
- 大部分回调使用实际无需通过协议暴露给外部。
二、Block
Block是OBJC提供的一种运行时方法机制,由c函数实现,它提供了一种运行时的临时回调机制。
Block对象的声明:
声明一个参数为int,返回值为int,带2个int参数的Block对象block。
<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> (^block)(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
通过typedef简化
<code class="hljs d has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> (^block)(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>); block bl = ^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> a,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> b){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> a+b; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
* 回调函数定义:*
<code class="hljs erlang has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-pp" style="box-sizing: border-box;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(int)</span>handleBlockCallbackFunc: <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(block)</span>callback { return callback<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>)</span>; }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
回调函数使用:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> ret = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> handleBlockCallbackFunc: ^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> param,__unused <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> b) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Block Msg: %d"</span>, param); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> param*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>; }];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
Warning
- block对象使用的变量、参数在运行时被绑定,因此可以直接使用栈空间建立的变量,无需参数传入。但block对象的创建依然有生命周期限制,因此传入异步调用的block对象时,如果是栈空间创建的block,必须
使用Block_copy()将block拷出备份,然后使用Block_release()将block释放。 - 对于在栈空间声明的变量,绑定到block时被标记为const。只能读取不能写入。如果需要写入,需要用__block对变量进行标记。此时block使用的是从栈拷贝到堆中的对象。当出block时,如果栈可用则将堆中对象自动拷贝回栈。
优点:
- 最轻量级的回调机制
- 编译器类型检查
- 如函数指针一样,灵活定义回调函数
缺点:
- 执行效率。(影响程度不清楚)
- 容易导致代码逻辑集中
- IOS4之后的特性(现在不用考虑!)
三,respondsToSelector和performSelector
利用RunTime特性进行回调
优点
- 与OBJC代码兼容性好
- 具有延迟执行等特性
- 轻量级的回调机制
缺点
- 回调产生的返回值只能为id类型,基本数据类型会产生错误
- 参数最多只能传入两个。但可以通过建立包含多个参数的参数类进行回避。同时返回值限制也可通过此方式解决,即建立一个输入类和一个输出类。NSInvocation也提供了多参数的解决方法
- 如果以
<code class="hljs ini has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 102, 102);">[target performSelector: @selector(callback)]</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
方式建立回调,则需要对类的回调消息名建立约定,且回调消息名具有独占性,即一个类中只能以此消息名进行回调。
如果通过外部传入SEL建立回调
<code class="hljs ini has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 102, 102);">[target performSelector: sel]</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
或是外部传入字符串建立回调
<code class="hljs ini has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 102, 102);">[target performSelector:NSSelectorFromString(@"callback")]</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
使用自动引数编译器特征(ARC)会产生警告“performSelector may cause a leak because its selector is unknown”
使用此种方式建立回调,当传入一个不符合约定的消息时,会产生副作用继续运行,而非报错。比如约定消息有2个参数,但传入消息只有1个参数,则按照参数约定顺序屏蔽掉最后传入的参数。或是传入消息具有3个参数,则多余的参数值未初始化。
四,函数指针
C语言回调机制。
优点:
- 轻量级的回调机制
- 只约定返回值和参数,而非函数名。无参数、返回值限制,使用灵活
- 编译器提供类型检查。(错误时产生警告)
缺点 - 与OBJC的消息机制不兼容。因为消息并非C语言中那样,函数名对应函数指针。即只能对C函数进行回调
- 传入不符合约定的函数指针时,产生副作用继续运行,而非报错
五,objc_msgSend
需要导入#import
<code class="hljs r has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">id objc_msgSend(id theReceiver, SEL theSelector, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
优点:
- 轻量级的回调机制
- 无传入参数限制
- 相比performSelector,使用自动引数特征时,不产生警告
- 同系列的方法支持double、struct等类型的返回值,但仍然不支持int型返回值(可使用NSNumber包装以回避)
缺点: - 传入不符合约定的消息时,产生副作用继续运行,而非报错
六,IMP
类似于OBJC提供的函数指针,它通过methodForSelector方法查询传入的Selector,以获得函数的入口地址,一般不用RunTime特性几乎用不到!
<code class="hljs r has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> id (*IMP)(id, SEL, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
相比普通C语言的函数指针,其定义多了id,SEL这两个强制参数约定
优点:
- 轻量级的回调机制
- 传入不符合约定的消息时,报错
- 无传入参数限制。返回值可通过强转获得,无类型限制
typedef int (*CBFUNC)(id, SEL, int, int, int); // 定义函数指针类型
int ret = ((CBFUNC)callback)(self, sel, param1, param2, param3); // 强制转换
这里的id和SEL只是OBJC系统约定的占位,自定义回调时无实际意义
缺点:
- 不能提供如同协议和函数指针的编译期类型检查
七,NSNotificationCenter
NSNotificationCenter是OBJC提供的消息机制。它有些类似于观察者模式,通过关注感兴趣的消息,建立回调。NSNotificationCenter提供了一种低耦合的对象通讯机制,特别适合无指定对象的一对多回调。
1)获取消息中心实例(系统已创建,单件模式)
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotificationCenter</span> *nc = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotificationCenter</span> defaultCenter]; <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)发送消息。(事件发生时调用) </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotificationCenter</span> *nc = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotificationCenter</span> defaultCenter]; [nc postNotificationName: NOTIFY_MSG_UC_COMMON_PLAYER_PLAY <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 消息名(字符串)</span> object:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 消息源</span> userInfo:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 用户字典(传递更多自定义参数)</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
3)注册消息
<code class="hljs oxygene has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> [nc addObserver: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 观察者</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">selector</span>: @<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">selector</span>(handleNotify_Play:) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 回调</span> name: NOTIFY_MSG_UC_COMMON_PLAYER_PLAY <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 监听消息</span> object: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nil</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 消息源</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
4)注销消息
<code class="hljs ini has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 102, 102);">[nc removeObserver: self]</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
5)回调定义
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>) handleNotify_Play:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotification</span> *)note; 只有一个参数 <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNotification</span>* –name <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 消息名</span> –object <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 消息源</span> –userInfo <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 用户字典</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
优点:
- 回调对象间耦合度低。相互之间可不必知道对方存在
- 通过消息传递的信息无限制
-
观察者可选择特定消息、特定对象,或者特定对象的特定消息进行观察
缺点:
-
缺乏时序性。当事件发生时,回调执行的先后次序不确定。也不能等待回调完成执行后续操作。解决:1)使用传统回调机制。2)多线程时,可使用NSCondition同步线程。3)使用更多的消息。(过多使用可能导致混乱)
总结:
OBJC还没有太完美的轻量级回调机制,只能根据情况选择合适的机制
- 单纯的回调,且没有复用的必要,也无IOS版本限制,可采用block
- 单纯的回调,有复用要求,可使用performSelector、objc_msgSend,或是IMP的回调机制
- 使用自动引数的情况下,尽量不使用performSelector回调传入的@Selector,防止警告
- 对象间有较多的互操作,对象有复用的必要,可采用协议
- 无指定对象的一对多回调采用NSNotificationCenter。
- 有延迟调用等特殊应用的,可以使用performSelector