设置Cocoa应用程序退出状态的任何好方法吗? [英] Any good way to set the exit status of a Cocoa application?

查看:76
本文介绍了设置Cocoa应用程序退出状态的任何好方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

不幸的是,我一直没有找到一个好的方法-NSApplication似乎没有任何设置退出状态的方法.此刻,我已经将NSApplication子类化,并添加了exitStatus ivar(在必要时在我的应用程序委托中设置了它),然后覆盖了-terminate:,以便它调用exit(exitStatus).这样做很好,但是对我来说似乎有点烦恼,更不用说我可能会错过标准terminate:在幕后所做的重要事情.我不能在子类化方法中调用[super terminate:sender],因为exit()不会给我设置状态的机会.

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.

我缺少明显的东西吗?

推荐答案

简而言之,您可以调用exit(3)并完全绕过标准的可可拆卸机制退出代码(无需像您描述的那样跳入箍圈.

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.

正如杰森(Jason)所提到的,在应用程序拆除期间,没有任何对系统至关重要的 .另一方面,您的应用程序可能有一些关键问题,但这完全是由于您的应用程序的实现(默认情况下,Cocoa中没有任何内容).

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.

这篇关于设置Cocoa应用程序退出状态的任何好方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆