什么是CFNotificationCenter的回调函数观测? [英] what is the CFNotificationCenter callback function observer?

查看:656
本文介绍了什么是CFNotificationCenter的回调函数观测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来CFNotificationCenter的。我读在developer.apple的文档,它说,在callbackFunction参数的观测是非NULL标识观察者以外的任意值。观察者可以是一个对象,这样我可以访问回调函数内它的方法?
这些参数如图所示的文件中。

I'm new to the CFNotificationCenter. I was reading the documentation at developer.apple and it says that in the CallbackFunction that observer is "an arbitrary value other than NULL that identifies the observer". Can the observer be an object so that I can access its methods inside the callback function? These are the parameters as shown in the documentation.

   void MyCallBack (
       CFNotificationCenterRef center,
       void *observer,
       CFStringRef name,
       const void *object,
       CFDictionaryRef userInfo
    );

另外一个问题。为什么允许观察员加入观察者??时要NUL

Another question. Why is it allowed for an observer to be nul when adding an observer??

void CFNotificationCenterAddObserver (
   CFNotificationCenterRef center,
   const void *observer,
   CFNotificationCallback callBack,
   CFStringRef name,
   const void *object,
   CFNotificationSuspensionBehavior suspensionBehavior
);

的观察员,在OS X 10.3及更高版本,这个参数可以为NULL。

"The observer. In OS X v10.3 and later, this parameter may be NULL."

推荐答案

观察的说法是,你传递给一个任意指针 CFNotificationCenterAddObserver ,它与你的回调指针存储一起。当通知中心呼唤你的回调,它通过观察指针作为参数传递给回调。

The observer argument is an arbitrary pointer that you pass to CFNotificationCenterAddObserver, which it stores along with your callback pointer. When the notification center calls your callback, it passes the observer pointer as an argument to the callback.

就是这样。通知中心不取消引用观察指针,还是尽量保留。这是一种让你传递额外的信息给你的回调,如果你需要。如果你想用它来引用一个Objective-C的对象,就可以了。您应该确保,而观察者所注册的对象不会被释放。

That's it. The notification center doesn't dereference the observer pointer, or try to retain it. It's a way for you to pass extra info to your callback if you need to. If you want to use it to reference an Objective-C object, you can. You should make sure the object won't be deallocated while the observer is registered.

这篇关于什么是CFNotificationCenter的回调函数观测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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