简体   繁体   English

Objective C委托还是C风格的块回调?

[英]Objective C delegate or C-style block callback?

I am designing a class that will "fire events" whenever something occurs. 我正在设计一个类,当发生某些事情时,它会“触发事件”。 These events tend to be non-UI related. 这些事件往往与非UI相关。 I'm wondering what the best method for doing so is. 我想知道这样做的最佳方法是什么。 I've been exploring either: 我一直在探索:

Delegates 代表

I'll define a delegate class, accept a delegate in the init function, and call the methods on the delegate class when an event occurs. 我将定义一个委托类,在init函数中接受委托,并在事件发生时调用委托类上的方法。

C-style blocks C风格的街区

I'll define a function pointer, and accept a function in the init function. 我将定义一个函数指针,并在init函数中接受一个函数。 I'll call it when an event occurs. 我会在事件发生时调用它。

In both cases, I may need to handle multiple "sources" so I'll need an array of delegates or blocks. 在这两种情况下,我可能需要处理多个“源”,因此我需要一组委托或块。

I've noticed that in iOS programming, delegates tend to be preferred especially for UI frameworks. 我注意到在iOS编程中,代表往往更喜欢UI框架。 But I come from a functional programming background where I definitely am comfortable with accepting function points and passing lambdas at the call site, and I like that the compiler handles hoisting variables for you, and you generally need less class-state. 但是我来自函数式编程背景,我非常适合接受函数点并在调用站点传递lambdas,我喜欢编译器为你处理吊装变量,而且你通常需要更少的类状态。 But I see that a lot of iOS developers are using delegates. 但我发现许多iOS开发人员正在使用委托。

What is the generally preferred mechanism in iOS for doing this? iOS中通常首选的机制是什么?

Each has its use. 每个都有它的用途。

Delegates should be used when there are multiple "events" to tell the delegate about and/or when the class needs to get data from the delegate. 当有多个“事件”告诉代表时,和/或何时类需要从代理获取数据时,应使用代理。 A good example is with UITableView . 一个很好的例子是UITableView

A block is best used when there is only one (or maybe two) event. 当只有一个(或两个)事件时,最好使用块。 A completion block (and maybe a failure block) are a good example of this. 完成块(可能是故障块)就是一个很好的例子。 A good example is with NSURLConnection sendAsynchronousRequest:queue:completionHandler: . 一个很好的例子是NSURLConnection sendAsynchronousRequest:queue:completionHandler: .

A 3rd option is notifications. 第三个选项是通知。 This is best used when there are possibly multiple (and unknown) interested parties in the event(s). 当事件中可能存在多个(和未知的)相关方时,最好使用此方法。 The other two are only useful when there is one (and known) interested party. 其他两个仅在有一个(并且已知)感兴趣方时才有用。

Using delegates means more tightly coupling in terms of architecture than using simple callback blocks. 使用委托意味着在架构方面比使用简单的回调块更紧密地耦合。 And for non-complex cases delegates can be an overkill. 对于非复杂的情况,代表可能是一种过度杀伤力。

Storing blocks in some container is just ok, but you should think ahead about possibility of removing them at some point later (this will require some work), I mean an additional interface for removing of an already added handler. 在某个容器中存储块是正常的,但你应该提前考虑在以后的某个时候删除它们的可能性(这将需要一些工作),我的意思是用于删除已经添加的处理程序的附加接口。

For your use case NSNotification seems to be the best choice. 对于您的用例, NSNotification似乎是最佳选择。 Objects that need those events then can register for those notifications. 然后,需要这些事件的对象可以注册这些通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM