需要在我的 Firebreath 项目中添加 NSDistributedNotification 观察者
我需要从我的 cocoa 应用程序向我的 firebreath 项目发送分布式通知,因此我需要在我的 firebreath 代码中创建一个观察者和一个选择器。 我将类扩展名更改为“.mm”以支持 Objective-C 代码。我的 firebreath 项目中已经有 Objective-C 代码并且工作正常。但是当我尝试创建观察者时,我的代码中出现错误,并且我不知道如何解决它。
这是我的 firebreath 项目的源代码:
//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];
}
这是我的错误:
...firebreath/../projects/MyProject/MyProjectAPI.mm:133: 错误:在“-”标记之前预期有不合格的 id。这是我定义“receiveAppConfirmationNotification”方法的行。
...firebreath/../projects/MyProject/MyProjectAPI.mm:157:错误:“self”未在此范围内声明。
我该如何定义选择器? 我该如何将观察者添加为类本身?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择器必须是 Objective-C++ 类的一部分;你不能把它扔到某个地方,它应该放在类的 @implementation 部分。
为了尽可能保持兼容,我建议将 @interface 和 @implementation 部分都放在 .mm 文件中,以便 .h 文件与 C++ 兼容,但这取决于您;如果你这样做的话会更容易。如果需要,您可以使用 pimpl 模式来帮助解决此问题。
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.
我做了接口和实现,代码没有错误。问题是我能够向我的可可应用程序发送通知,但我无法从应用程序向插件接收通知。
这是头文件:
这是我的源文件:
这是我来自 cocoa 应用程序的源文件:
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:
Here is my source file:
Here is my source from the cocoa application: