2011-12-30 27 views
0

我需要從我的可可應用程序向我的firebreath項目發送一個分佈式通知,所以我需要在我的firebreath代碼中創建一個觀察者和一個選擇器。 爲了支持Objective-C代碼,我將類擴展名更改爲「.mm」。我的firebreath項目中已經有Objective-C代碼,並且工作正常。但是當我嘗試創建觀察者時,我的代碼中出現錯誤,我不知道如何解決它。需要在我的Firebreath項目中添加一個NSDistributedNotification觀察者

這裏是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 /../項目/ MyProject的/ MyProjectAPI.mm:133:錯誤:預期' - '標記之前的非限定標識。這是我定義「receiveAppConfirmationNotification」方法的路線。

... firebreath /../ projects/MyProject/MyProjectAPI.mm:157:錯誤:'self'未在此範圍內聲明。

如何定義選擇器? 我該如何添加觀察者作爲類本身?

回答

0

選擇器必須是Objective-C++類的一部分;你不能把它扔在那裏,它應該在類的@implementation部分。

爲了保持事物儘可能兼容,我建議在.mm文件中同時添加@interface和@implementation部分,以便.h文件與C++兼容,但這取決於您;如果你這樣做,它會更容易。如果你願意,你可以使用pimpl模式來幫助你。

+0

有使用客觀C對象的幾個例子在firebreath codebase中使用firebreath。 – taxilian 2011-12-30 21:13:01

0

我做了接口和實現,代碼沒有錯誤。問題是我能夠向我的可可應用程序發送通知,但我無法從應用程序向插件重新發送通知。 這裏是頭文件:

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

下面是從可可應用我的源:

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

如果我刪除此行「[my_project發佈];」通知有效,但我需要釋放內存。我也不能做autorelease。有任何想法嗎? – Ana 2012-01-03 17:57:48

相關問題