Run NSRunLoop in a Cocoa command-line program

前端 未结 6 1696
遥遥无期
遥遥无期 2020-11-27 04:20

Is it possible to initialize a NSRunLoop without loading any NIB files (i.e., without calling NSApplicationMain())?

Thanks.

6条回答
  •  盖世英雄少女心
    2020-11-27 04:58

    In Swift, you can achieve this by appending the following line to the end of your main.swift:

    NSRunLoop.currentRunLoop().run();  // Swift < 3.0
    RunLoop.current.run();             // Swift >= 3.0
    

    If you want to be able to stop the run loop you have to use the Core Foundation methods.

    CFRunLoopRun(); // start
    

    And you can stop it like this

    CFRunLoopStop(CFRunLoopGetCurrent()); // stop
    

提交回复
热议问题