Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 如何在手动使用nsrunlop时接收NSN通知_Cocoa_Nsnotificationcenter_Nsrunloop - Fatal编程技术网

Cocoa 如何在手动使用nsrunlop时接收NSN通知

Cocoa 如何在手动使用nsrunlop时接收NSN通知,cocoa,nsnotificationcenter,nsrunloop,Cocoa,Nsnotificationcenter,Nsrunloop,我可以在GUI程序的应用程序委托中观察NSWorkspaceWillPowerOffNotification通知,但不能在命令行实用程序中观察。你知道为什么下面的方法不起作用吗?还是我应该考虑另一种方法 编辑:是否NSWorkspace的通知中心不适用于无GUI程序?(我已确认“通知中心”不是零) 您可以,但对于已启动的进程,请参见:啊,我明白了。是的,是一个发射代理。谢谢你的信息! @interface Helper : NSObject -(void)userLogout:(NSNotifi

我可以在GUI程序的应用程序委托中观察NSWorkspaceWillPowerOffNotification通知,但不能在命令行实用程序中观察。你知道为什么下面的方法不起作用吗?还是我应该考虑另一种方法

编辑:是否NSWorkspace的通知中心不适用于无GUI程序?(我已确认“通知中心”不是零)


您可以,但对于已启动的进程,请参见:啊,我明白了。是的,是一个发射代理。谢谢你的信息!
@interface Helper : NSObject
-(void)userLogout:(NSNotification*)notification;
@end

@implementation Helper
-(void)userLogout:(NSNotification*)notification
{
    NSLog(@"Notification Received");
}
@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Helper* helper = [[Helper alloc] init];

        NSNotificationCenter* notificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
        [notificationCenter addObserver:helper
                               selector:@selector(userLogout:)
                                   name:NSWorkspaceWillPowerOffNotification
                                 object:nil];

        NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
        [runLoop run];
    }
    return 0;
}