How to properly quit application, call exit(0)?
Asked Answered
G

3

5

I'm not sure which is the right way, right now I'm just calling exit(0) when a user clicks on the exit button.

Gregorio answered 17/11, 2009 at 13:32 Comment(1)
Don't forget to title your button “Quit”, not “Exit”.Vas
B
24

Use NSApplication's terminate method. For example:

[NSApp terminate:self]

Documentation is available here.

Bifocals answered 17/11, 2009 at 13:35 Comment(0)
S
6

The previously given answer is deprecated since 10.10 and this question is the first thing that turns up for cocoa quit application, so use this instead (Swift):

NSApplication.sharedApplication().terminate(self)

Note: As of now it's the following:

NSApplication.shared().terminate(self)
Shanklin answered 17/4, 2015 at 19:0 Comment(2)
What's deprecated? The use of NSApp? NSApp still appears to be available and isn't marked as deprecated in the documentation…Bifocals
Because I just had to do this again: the original way to do it now yields: static member 'sharedApplication' cannot be used on instance of type 'NSApplication'. PS: Note that I wrote the comment above when Swift was still quite unstable, and especially Xcode was quirky as hell. So the error message might have been different at the time.Shanklin
D
0

I always terminate App the safe way.

[NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];

This will put the event in the next loop.

Durban answered 17/4, 2015 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.