大多数Apple框架中的回调都是通过应用程序的主运行循环传递的。如果您的命令行工具没有运行循环,则它将无法接收以这种方式发送的回调。
如果没有runloop,则框架调用回调的唯一方法是在另一个线程上运行它,这可能会导致意外的应用程序异常行为。
添加以下内容就足够了:
let runLoop = RunLoop.currentlet distantFuture = Date.distantFuturewhile running == true && runLoop.run(mode: RunLoopMode.defaultRunLoopMode, before: distantFuture) {}


