Reputation: 420
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??
Upvotes: 1
Views: 1121
Reputation: 237110
As Ben S pointed out, you can't use a C++ object where an Objective-C object is expected. They're just not the same thing. If you're targeting 10.6, one alternative to making an Objective-C wrapper is to use NSNotificationCenter's addObserverForName:object:queue:usingBlock:
. Just give it a block that calls a function on your object and you get the same effect as adding that object as an observer.
Just to emphasize, though, that is only possible on 10.6.
Upvotes: 0
Reputation: 69412
No, you can't do that.
The Objective-C and C++ class hierarchies are separate. In a C++ context, self
is not defined but this
is defined. In an Objective-C context, this
is not defined and self
is.
Also, addObserver
takes an id
parameter. C++ object instances are not considered of type id
so even if you change your code to pass this
instead of self
it 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.
Upvotes: 6