代码块是否完全取代了委托? [英] Do code blocks completely replace delegates?

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

问题描述

现在 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 中公开的新类以及对预先存在的类所做的添加的审查,我建议我的团队假定委托协议,但在存在 直接因果关系时提供块代替em> 在特定的调用和块的性能之间.

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.

所以,一些例子:

UIScrollViewDelegate 是正确的委托协议,因为 (i) 可能需要传达的内容范围很广;(ii) 他们可能需要在任何时间以任何顺序进行沟通;(iii) 他们将因受托人无法控制的原因进行沟通.

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天全站免登陆