NSNotificationCenter 与委托(使用协议)?

发布于 2024-08-15 03:14:53 字数 39 浏览 5 评论 0原文

它们各自的优点和缺点是什么?
我应该在哪里具体使用它们?

What are the pros and cons of each of them?
Where should I use them specifically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

芯好空 2024-08-22 03:14:53

这里的经验法则是有多少客户希望收到事件通知。如果它主要是一个对象(例如,关闭视图或对单击的按钮进行操作,或对失败的下载做出反应),那么您应该使用委托模型。

如果您发出的事件可能同时引起许多对象的兴趣(例如屏幕旋转、内存使用、用户登录/注销),那么您应该使用NSNotificationCenter

The rule of thumb here is how many clients would like to be notified of an event. If it's mainly one object (e.g. to dismiss a view or to act upon a button clicked, or to react to a failed download) then you should use the delegate model.

If the event you emit may be of an interest to many objects at once (e.g. screen rotated, memory usage, user login/logout), then you should use the NSNotificationCenter.

幸福不弃 2024-08-22 03:14:53

它们的目的不同:

  • 通知用于向可能是发送者未知的多个接收者广播消息。

  • 委派用于将消息发送到代表发件人行事的单个已知收件人。

Their purposes are different:

  • Notification is used to broadcast messages to possibly several recipients unknown from the sender.

  • Delegation is used to send messages to a single known recipient acting on behalf of the sender.

新雨望断虹 2024-08-22 03:14:53

通知通常更适合通知 UI 其他线程上发生的更改。出于稳定性和性能方面的原因,Apple 的文档强烈反对尽可能跨线程使用委托。在 Mac 上,他们建议使用绑定,但由于 iPhone 上不存在绑定,因此通知可能是您的下一个最佳选择。

Notifications are generally better for notifying the UI of changes the occur on other threads as well. Apple's documentation strongly discourages the use of delegates across threads where possible, both for stability and performance reasons. On the Mac, they suggest using Bindings, but since they don't exist on the iPhone, notifications are probably your next best bet.

萝莉病 2024-08-22 03:14:53

考虑性能是一个好主意(对于少量通知对象来说,委托更好,对于大量对象来说,通知中心更好,或者是吗?运行探查器),但我认为一个更重要的因素,因为你正在谈论 Objective-C 和不太可能谈论代码库中真正高性能的部分(这些部分可能是用 C 编写的)正在减少模块之间的编译时依赖性。

没有什么可以阻止您拥有一组委托而不是单个委托。

我可能仅将 NSNotificationCenter 用于我制作的任何网络堆栈组件的状态以及任何自定义设备状态监控接口。但对于大多数耦合,与应用程序的全局状态无关,我认为在大多数情况下使用 Objective-C 中的普通接口契约比使用 NSNotificationCenter 更清晰,并且对于追随你的人来说更容易遵循。事实上,我从未将NotificationCenter 用于我自己的自定义事件,并且更喜欢使用委托来方便阅读我的代码的其他人理解代码。

最后,当然,对于来自标准 API 的通知,您别无选择,必须使用 Apple 针对给定事件禁止的两种方法中的任何一种。

Considering the performance is a good idea (delegation better for small number of notified objects, notification centre better for larger number of objects, or is it? run a profiler) but I think a more important factor since you are talking about Objective-C and less likely to be talking about the really high performance parts of your code base, which are likely to be written in C, is reducing compile-time dependencies between modules.

There is nothing to stop you having an array of delegates rather than a single delegate.

I might use NSNotificationCenter only for status of any network stack components I make and any custom device status monitoring interfaces. But for most coupling, not to do with global status of the app, I think it is clearer to use normal interface contracts in Objective-C in most cases and easier to folow for the people coming after you than to use NSNotificationCenter. In fact I have never used NotificationCenter for my own custom events and prefer to use delegates for ease of code comprehension by someone else reading my code.

And finally, of course with notifications to/from the standard API you don't have choice and must use whichever of the two methods Apple proscribe for a given event.

陈独秀 2024-08-22 03:14:53

通知更适合解耦 UI 组件。它允许您插入任何视图,而无需对控制器或模型进行任何修改。对于松散耦合的设计来说绝对更好。

但对于委托和通知之间的性能,你需要考虑调用的频率。

对于较频繁的事件,委派可能会更好;对于不太频繁的事件但收件人较多的情况,通知可能会更好。选择什么取决于项目。

Notifications are better for decoupling UI components. It allows you to plug any view without any modification in your controllers or models. Definitely better for loosely-coupled design.

But for the performance between delegation and notification, you need to think about the frequency of the call.

Delegation might be better for more frequent events, notifications are better for less frequent events but more recipients. It's up to project what to pick.

桃气十足 2024-08-22 03:14:53

介于这两者之间的一个选项是使用观察者模式,而不使用 NSNotificationCenter。在此处查看我的 Objective-C 实现。

An option in between those two is using the observer pattern, without NSNotificationCenter. Look at my Objective-C implementation here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文