需要在我的 Firebreath 项目中添加 NSDistributedNotification 观察者

发布于 2024-12-23 09:18:18 字数 1172 浏览 2 评论 0原文

我需要从我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉睡月亮 2024-12-30 09:18:18

选择器必须是 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.

痴情换悲伤 2024-12-30 09:18:18

我做了接口和实现,代码没有错误。问题是我能够向我的可可应用程序发送通知,但我无法从应用程序向插件接收通知。
这是头文件:

#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

这是我的源文件:

@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;
}

这是我来自 cocoa 应用程序的源文件:

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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文