惯用Swift复用通知? [英] Idiomatic Swift multiplex notifications?

查看:121
本文介绍了惯用Swift复用通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义一个类结构,需要在有趣的事情发生时通知消费者(获取新数据等等)。与代表关系类似,但可能有很多消费者。和一个更直接的关系,而不是将一个 NSNotification 投入风中。

I'm defining a class structure that needs to notify its consumers when interesting things happen to it (gets new data, and so on). Similar to a delegate relationship, but there may be many consumers. And a more direct relationship than casting an NSNotification into the wind.

我在大多数Objective-C程序中的倾向只需定义一个协议,并让这些消费者实现它并注册为 id< MyProtocol> ;我会将它们填入一个 NSMutableSet ,我会在适当的时刻迭代。我花时间写C#的时候会倾向于尝试修改这种方法,以便在Swift中使用泛型(a la private var myConsumers = Set< MyProtocol>())。事实证明是一个黑暗和痛苦的兔子洞,据我所知。但是不要再潜入它,请让我备份并尝试解决真正的问题。

My inclination in most Objective-C programs would just be to define a protocol, and let those consumers implement it and register themselves as id<MyProtocol>; I'd stuff them into an NSMutableSet, which I'd iterate over at opportune moments. My time spent writing C# would incline me to try to modify that approach somewhat to use generics in Swift (a la private var myConsumers = Set<MyProtocol>()). That turns out to be a dark and painful rabbit hole, as far as I can tell. But rather than dive into it, let me back up and try to solve the real problem.

所以:我有一些类,需要通知0-N消费者有趣的事情发生。允许这些消费者注册和注册自己是很好的,因为他们的寿命可能比我的班级短。

So: I have some class where instances will need to notify 0-N consumers of interesting things that happen. It'd be nice to allow those consumers to register and de-register themselves, since their lifespans may be shorter than my class.

什么是惯用的Swift方法来实现这种模式?

What is the idiomatic Swift approach to implementing this pattern?

推荐答案

Cocoa中多代表团的惯用方法是通知,具体使用 addObserverForName对象:队列:usingBlock:)。通过你的关闭。保存返回值,并使用该返回值在将来取消注册。

The idiomatic solution to multi-delegations in Cocoa is notifications, specifically using addObserverForName(object:queue:usingBlock:). Pass your closure. Save the return value, and use that return value to unregister yourself in the future.

有ObjC组播代理( Robbie Hanson的可能是我最喜欢的,我已经实现了),但大多数时候它的麻烦比这是值得的。通知是Cocoa如何管理非代表观察员。

There are ObjC multicast delegates (Robbie Hanson's is probably my favorite outside of the ones I've implemented), but most of the time its more trouble than its worth. Notifications are how Cocoa manages non-delegate observers.

这里的非委派是指通知的东西,但从不问任何事情。 多代表的问题是真的是一个废话。 代表是你要求的信息;你代表你的决定的责任。观察员不是真正的代表(虽然代表有时是观察者)。你不要问他们什么。他们无法控制决策。所以多代表是废话。多少事情如何能够回答问题呢?为了使这个更具体,委托人有非 void 方法。考虑为 UITableViewDelegate 提供多代表意味着什么。你如何决定行的高度?

"Non-delegate" here means "things that are notified, but never asked anything." The problem with "multi-delegate" is that it is really a nonsense term. A "delegate" is something you ask for information; something you "delegate" responsibility for a decision to. Observers aren't really "delegates" (though delegates are sometimes observers). You don't ask them anything. They have no control over decision making. So a multi-delegate is nonsense. How can multiple things all be responsible for answering a question? To make this more concrete, delegates have non void methods. Consider what it would mean to have a "multi-delegate" for UITableViewDelegate. How would you decide what the row height was?

如果你正在构建自己的,我可能会推荐 Dictionary< Key,() - > ; Void> 其中key是您返回给调用者以后删除的句柄。这样你就不必担心关闭平等的疯狂(我同意克里斯·拉特纳是一个非常开放,可能是无法解决的问题)。

If you are building your own, I would probably recommend Dictionary<Key, ()->Void> where "key" is the handle you return to the caller for later removal. That way you don't have to worry about the craziness of closure equality (which I agree with Chris Lattner is a very open-ended, and possibly unsolvable, question).

这篇关于惯用Swift复用通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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