Any good way to set Cocoa app exit status?

I have a Cocoa application that interacts with a server and displays a graphical interface. If there is a fatal error, I show a warning and exit. I would like to set the exit status to a non-zero value to reflect that an error has occurred, for the convenience of interacting with some other UNIX-based tools.

Unfortunately, I could not find a good way to do this - NSApplication seems to have no way to set the exit status. At the moment, I have subclassed NSApplication and added exitStatus ivar (which I set the application delegate to when I need it), then reimplemented -terminate: so that it exit(exitStatus) . It works great, but for me it seems a little rough, not to mention that I might miss something important that the standard terminate: does behind the scenes. I cannot call [super terminate:sender] in my subclass method, because exit() does not allow me to set the status.

Am I missing something obvious?

+4
source share
2 answers

In short, you either call exit(3) , or completely bypass Cocoa's standard reset mechanisms, or you cannot set the exit code (without going through the hoops as you describe).

As Jason mentioned, there is nothing important for the system that occurs during the failure of the application. On the other hand, your application may have something critical, but this is solely due to the implementation of your application (and not the default in Cocoa).

But, in fact, do not do this - the user likes to forcibly leave the grudge, and your application should be designed so that in the end it is not catastrophically catastrophic.

+5
source

There are other cleaner methods for interacting with UNIX-based tools. For example, you can simulate an exit code by writing the code in a text file, from where tools can pick it up without hacking your application to return the code. You could also use the stdout / console application.

+1
source

Source: https://habr.com/ru/post/1308426/


All Articles