代码块是否完全替代代理? [英] Do code blocks completely replace delegates?

查看:91
本文介绍了代码块是否完全替代代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,对于iphone / ipad开发,最终支持这些功能块,这样做是否完全消除了对代理的需求,或者代理人作为一个完整的接口实现更为清洁,而块更适合于单个任务?

Now that blocks are finally supported for iphone/ipad development, do these completely remove the need for delegates or are delegates still cleaner as a complete interface implementation while blocks are more appropriate for single tasks?

推荐答案

我一直在搜索官方文档,但尚未找到任何内容。基于审查在iOS 5中暴露的新课程和对现有课程的添加,我建议我的团队推定一个委托协议,但是当有一个直接因果关系

I've been searching for official documentation on this but have yet to find any. Based on reviewing the new classes exposed in iOS 5 and those additions made to pre-existing classes, I'm recommending to my team to presume a delegate protocol but to provide a block instead when there is a direct causal nexus between that specific call and performance of the block.

所以,根据经验,一个委托协议是正确的,当需要正在进行的通信时,这个动作导致通信是扩散的,或者动作是从第三个来源发出的。块主要与异步操作和单击类特别相关。

So, as a rule of thumb, a delegate protocol is correct when there's a need for ongoing communication, the actions causing the communication are diffuse or the actions are emanating from a third source. Blocks are primarily associated with asynchronous actions and single-shot classes in particular.

编辑:所以,一些例子:

so, some examples:

UIScrollViewDelegate 正确地是委托协议,因为(i)可能需要传达的各种各样的事情; (ii)他们可能需要以任何顺序和任何时间传达;

UIScrollViewDelegate is correctly a delegate protocol because (i) there's a wide range of things that may need to be communicated; (ii) they may need to be communicated in any order and at any time; and (iii) they will be communicated for reasons beyond the delegate's control.

NSURLConnection + sendAsynchronousRequest:queue:completionHandler:正确地采取一个块来交付结果,因为(i)只有一个结果报告;和(ii)报告是由呼叫者采取的行动的直接后果。

NSURLConnection +sendAsynchronousRequest:queue:completionHandler: correctly takes a block to deliver results because (i) there's exactly one result to report; and (ii) the report comes back as a direct consequence of action taken by the caller.

这篇关于代码块是否完全替代代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆