I would like to add notification center from a cpp class, is it possible????
If so, how do I do it?
Manager.h
class Manager
{
Manager();
};
Manger.mm file
Manager:Manager()
{
[[NSNotificationCenter dafaultCenter] addObserver:self selector:@selector(workermethod) name:@" Name" object:(id)nil];
}
My compiler gives an error saying self is not declared…….yes i know it is an obvious error.
Since I’m not deriving from NSObject….
Please let me know if it is possible to add notification center to cpp class in Cocoa??
No, you can’t do that.
The Objective-C and C++ class hierarchies are separate. In a C++ context,
selfis not defined butthisis defined. In an Objective-C context,thisis not defined andselfis.Also,
addObservertakes anidparameter. C++ object instances are not considered of typeidso even if you change your code to passthisinstead ofselfit won’t compile.See the C++ Limitations from the Objective-C Programming Language Manual.
As a workaround, create an Objectve-C class which has an instance of the C++ object you want the notification to go to, and simply call the C++ method on it when the Objective-C object gets the notification.