问题
I need to send a distributed notification from my cocoa app to my firebreath project, so I need to create an observer and a selector in my firebreath code. I changed the class extension to ".mm" in order to support objective-c code. I already have objective-c code in my firebreath project and is working ok. But when I try to create an observer I get errors in my code and I don't know how to resolve it.
Here is my source code from firebreath project:
//This is the selector
- (void)receiveAppConfirmationNotification:(NSNotification*)notif{
//The application is alive.
NSLog(@"The application is alive!!!!!!!!");
}
std::string MyProjectAPI::bgp(const std::string& val)
{
//Add an observer to see if the application is alive.
NSString *observedObject = @"com.test.net";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector(receiveAppConfirmationNotification:)
name: @"App Confirmation Notification"
object: observedObject];
}
Here are my errors:
...firebreath/../projects/MyProject/MyProjectAPI.mm:133: error: expected unqualified-id before '-' token. This is the line where I defined the "receiveAppConfirmationNotification" method.
...firebreath/../projects/MyProject/MyProjectAPI.mm:157: error: 'self' was not declared in this scope.
How can I do to define the selector? How can I do to add the observer as the class itself?
回答1:
A selector has to be part of an objective-c++ class; you can't just throw it in the middle of nowhere there, it should be in the @implementation section of the class.
To keep things as compatible as possible, I recommend putting both @interface and @implementation sections in the .mm file so that the .h file is compatible with C++, but that's up to you; it'll just be easier if you do it that way. You can use the pimpl pattern to help with this if you want.
回答2:
I did the interface and the implementation and the code is without errors. The problem is that I am able to send notifications to my cocoa application, but I am not able to recibe a notification from the application to the plugin. Here is header file:
#ifdef __OBJC__
@interface FBMyProject : NSObject {
NSString *parameter_val;
}
@property (nonatomic, retain) NSString *parameter_val;
-(void) receiveAppConfirmationNotification:(NSNotification*)notif;
@end
#endif
class MyProjectAPI : public FB::JSAPIAuto
{
public:
...
}
#endif
Here is my source file:
@implementation FBMyProject
@synthesize parameter_val;
-(void) receiveAppConfirmationNotification:(NSNotification*)notif{
//The application is alive.
NSLog(@"The application is alive!!!!!!!!");
}
- (id)init
{
self = [super init];
if (self) {
NSString *observedObject = @"test.com";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector(receiveAppConfirmationNotification:)
name: @"App Confirmation Notification"
object: observedObject];
}
return self;
}
- (void)dealloc
{
// unregister notification
[[NSDistributedNotificationCenter defaultCenter] removeObserver: self
name: @"App Confirmation Notification"
object: nil];
[self.parameter_val release];
[super dealloc];
}
@end
std::string MyProjectAPI::bgp(const std::string& val)
{
FBMyProject *my_project = [[FBMyProject alloc] init];
my_project.parameter_val = [NSString stringWithUTF8String:val.c_str()];
[my_project release];
return val;
}
Here is my source from the cocoa application:
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
@"OK", @"confirmation",
nil];
//Post the notification
NSString *observedObject = @"test.com";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center postNotificationName: @"App Confirmation Notification"
object: observedObject
userInfo: data
deliverImmediately: YES];
来源:https://stackoverflow.com/questions/8680145/need-to-add-a-nsdistributednotification-observer-in-my-firebreath-project