0

Mac で cmake を使用して単純な C++ アプリケーションを作成しています。C++ ソース コード ファイルには main があり、C++ クラスを作成します。このクラス内で、NSNotificationCenter のオブザーバーに自分自身を追加する目的の C オブジェクトを割り当てています。そして、私はそれらの通知を受け取っていません。コードがあります:

Notifications.h

class LaunchNotification {
public:
    LaunchNotification();
    virtual ~LaunchNotification();

    void StartNotifications();
    void StopNotifications();

private:
    void    *monitor;
};

通知.mm

@interface Monitor : NSObject
-(id) init;
-(void) appLaunchedNotification :(NSNotification *) notification;
-(void) appTerminatedNotification :(NSNotification *) notification;
@end

@implementation Monitor

- (id) init
{
    self = [super init];
    if (self)
    {
        count = 0;
        NSNotificationCenter *notCenter = [[NSWorkspace sharedWorkspace] notificationCenter];

        [notCenter addObserver : self
            selector:@selector(appLaunchedNotification:)
            name:NSWorkspaceDidLaunchApplicationNotification
            object:nil];

        [notCenter addObserver : self
            selector:@selector(appTerminatedNotification:)
            name:NSWorkspaceDidTerminateApplicationNotification
            object:nil];
    }

    return self;
}

- (void) appLaunchedNotification : (NSNotification *) notification
{
    NSString *path = [[notification userInfo]objectForKey: @"NSApplicationPath"];
}

- (void) appTerminatedNotification : (NSNotification *) notification
{
    NSString *path = [[notification userInfo]objectForKey: @"NSApplicationPath"];
}

- (void) dealloc
{
    NSNotificationCenter *notCenter = [[NSWorkspace sharedWorkspace] notificationCenter];

    [notCenter removeObserver : self];

    [super dealloc];
}
@end

LaunchNotification::LaunchNotification() : monitor(NULL)
{}

LaunchNotification::~LaunchNotification()
{
    StopNotifications();
}

void LaunchNotification::StartNotifications()
{
    if (NULL == monitor)
    {
        monitor = [[Monitor alloc] init];
    }
}

void LaunchNotification::StopNotifications()
{
    if (NULL != monitor)
    {
        [(id)monitor release];
    }
}
4

1 に答える 1