2012-02-25 239 views
11

如何让命令行工具永远运行。让命令行工具继续运行

这是我的代码:

#import <Foundation/Foundation.h> 

int main (int argc, const char * argv[]) 
{ 

    @autoreleasepool { 

     [[NSDistributedNotificationCenter defaultCenter] addObserverForName:nil object:nil queue:nil usingBlock:^(NSNotification *notification) 
     { 
      NSLog(@"%@", notification); 
     }]; 

     //Keep alive... 

    } 
    return 0; 
} 

回答

23

您需要进入使用或者CFRunLoop或NSRunLoop一个runloop。

尝试:

[[NSRunLoop currentRunLoop] run]; 
+0

它应该是'NSRunLoop'大写L,但否则它完美地工作。 – Tyilo 2012-02-26 00:39:43

+0

修正大写。 – EricS 2012-02-26 00:49:16

+0

就是这样,谢谢! – Andy 2015-12-26 18:53:21

0

在斯威夫特4,

RunLoop.current.run() 

干杯。

+0

为什么downvote?当我使用Swift保留命令行工具时,我发现了这个答案。我想有些人也想知道。 – 2018-01-03 11:43:25