2012-02-25 136 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