Objective c 需要在我的Firebreath项目中添加NSDistributedNotification观察员
我需要从cocoa应用程序向firebreath项目发送分布式通知,因此我需要在firebreath代码中创建一个观察者和选择器。 为了支持objective-c代码,我将类扩展名改为.mm。我已经在我的firebreath项目中使用了objective-c代码,并且工作正常。但当我试图创建一个观察者时,我的代码中会出现错误,我不知道如何解决它 以下是我来自firebreath项目的源代码:Objective c 需要在我的Firebreath项目中添加NSDistributedNotification观察员,objective-c,firebreath,Objective C,Firebreath,我需要从cocoa应用程序向firebreath项目发送分布式通知,因此我需要在firebreath代码中创建一个观察者和选择器。 为了支持objective-c代码,我将类扩展名改为.mm。我已经在我的firebreath项目中使用了objective-c代码,并且工作正常。但当我试图创建一个观察者时,我的代码中会出现错误,我不知道如何解决它 以下是我来自firebreath项目的源代码: //This is the selector - (void)receiveAppConfirmati
//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”
如何定义选择器?
如何将观察者添加为类本身?选择器必须是objective-c++类的一部分;你不能把它丢在无处的地方,它应该在类的@实现部分。
<>为了使事情尽可能兼容,我建议把.Cuffic和@实现部分都放在.mm文件中,以便.h文件与C++兼容,但这取决于你;如果你那样做会更容易。如果需要,您可以使用pimpl模式来帮助完成此操作。我完成了接口和实现,代码没有错误。问题是,我能够向cocoa应用程序发送通知,但无法接收应用程序向插件发送的通知。 下面是头文件:
#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];
在firebreath代码库中有几个将objective c对象与firebreath一起使用的示例;通知有效,但我需要释放内存。我也不能做自动释放。有什么想法吗?