相当于[NSNotificationCenter defaultCenter]的CF

时间:2013-06-15 11:10:47

标签: objective-c nsnotificationcenter

我想使用cpp函数作为观察者回调。 在CFNotificationCenterGet DarwinNotify / Distributed / Local 中心,CFNotificationCenterGetLocalCenter似乎最接近[NSNotificationCenter defaultCenter]。

CFNotificationCenterGetLocalCenter是否等同于[NSNotificationCenter defaultCenter]?

编辑:我有点怀疑,因为 答案here说 - 你不能通过C ++类添加观察者 但是那个here说 - 你可以通过CF函数在C ++类中添加观察者。

2 个答案:

答案 0 :(得分:1)

对通知使用C ++回调的最佳方法可能是使用-[NSNotificationCenter addObserverForName:object:queue:usingBlock:]对您的C ++代码使用小胶块。

答案 1 :(得分:0)

NSNotificationCenter和CFNotificationCenter 免费桥接(请参阅:definitive list of toll-free bridged types),因此,如果您尝试接收AppKit在由{{1}返回的特定NSNotificationCenter实例上发布的通知使用Objective-C似乎最安全,至少与NSNotificationCenter的实例进行交互。您的Objective-C类可以非常精简,只需调用您的C ++类,但如果这些是您希望接收的特定通知,则不会避免使用Objective-C。

编辑:根据经验,是的,根据您复制和粘贴的评论,向一个人发布通知似乎会导致另一个人收到通知。也就是说,我在CFNotificationCenter reference pageNSNotificationCenter reference page上都看到没有提及这种行为,也没有在Notification Programming Guide中提到这样做依赖在这样一个没有记录的实施细节上似乎存在风险。

相关问题