有什么好的方法来设置 Cocoa 应用程序的退出状态吗?

发布于 2024-08-30 10:53:41 字数 488 浏览 1 评论 0原文

我有一个 Cocoa 应用程序,它与服务器交互并显示 GUI。如果出现致命错误,我会显示警报并退出。我想将退出状态设置为非零值以反映发生了错误,以便于与其他一些基于 UNIX 的工具进行交互。

不幸的是我一直无法找到一个好的方法来做到这一点 - NSApplication 似乎没有任何方法来设置退出状态。目前,我已经对 NSApplication 进行了子类化,并添加了一个 exitStatus ivar(必要时在我的应用程序委托中设置),然后覆盖 -terminate: 以便它调用 <代码>退出(退出状态)。这工作正常,但对我来说似乎有点蹩脚,更不用说我可能错过了标准 terminate: 在幕后做的一些重要事情。我无法在子类方法中调用 [super Terminate:sender],因为 exit() 没有给我设置状态的机会。

我错过了一些明显的东西吗?

I have a Cocoa app which interacts with a server and displays a GUI. If there is a fatal error, I display an alert and exit. I'd like to set the exit status to a non-zero value to reflect that an error occurred, for ease of interaction with some other UNIX based tools.

Unfortunately I've been unable to find a good way to do so - NSApplication doesn't seem to have any way to set an exit status. At the moment, I've subclassed NSApplication and added an exitStatus ivar (which I set in my app delegate when necessary), then overridden -terminate: so that it calls exit(exitStatus). This works fine, but it seems a bit grungy to me, not to mention that I may be missing something important that the standard terminate: is doing behind the scenes. I can't call [super terminate:sender] in my subclassed method, because that exit()s without giving me a chance to set the status.

Am I missing something obvious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何其悲哀 2024-09-06 10:53:43

简而言之,您要么调用 exit(3) 并完全绕过标准的 Cocoa 拆卸机制或者您无法设置退出代码(无需跳过这些步骤)正如您所描述

的,在应用程序拆卸过程中,系统不会发生任何关键的事情,而另一方面,您的应用程序可能有一些关键的事情,但这完全是因为。你的应用程序的实现(默认情况下,Cocoa 中没有任何内容),

但是,实际上,不要这样做——用户出于恶意而喜欢强制退出,并且你的应用程序应该设计为不会因此而发生灾难性的失败。

In short, you either call exit(3) and completely bypass the standard Cocoa tear down mechanisms or you don't get to set the exit code (without jumping through the hoops as you describe.

As Jason mentioned, there isn't anything critical to the system that happens during app tear-down. Your app, on the other hand, may have something critical, but that is entirely because of your app's implementation (and not anything in Cocoa, by default).

But, really, don't do that -- user's love to force quit out of spite and your app should be engineered to not fail catastrophically as a result of that.

安静 2024-09-06 10:53:43

还有其他更简洁的方法可以与基于 UNIX 的工具进行交互。例如,您可以通过在文本文件中编写一些代码来模拟退出代码,工具可以从其中获取它,而无需破解您的应用程序以返回代码。也可以使用应用程序的标准输出/控制台。

There are other cleaner methods for interacting with unix based tools. For example, you could simulate the exit code by writing some code in a text file, from where the tools could pick it up, without hacking your app to return a code. It could also be possible to use the app's stdout/console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文