问题
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
);
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
);
"The observer. In OS X v10.3 and later, this parameter may be NULL."
回答1:
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.
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.
来源:https://stackoverflow.com/questions/11838753/what-is-the-cfnotificationcenter-callback-function-observer