To quit (close) a Mac app normally, choose Quit from the app's menu in the menu bar, or press Command (⌘)-Q. If the app doesn't quit, follow these steps to force the app to quit.
If your app adopts Metal features that are found only on Intel-based Mac computers, consider also adopting the iOS-specific features in your arm64 code. Adopting these features can lead to performance improvements for many apps. If your app uses Metal, OpenGL, or OpenCL, be aware of. Legacy Constants. Table 1 describes keys for an NSDictionary object containing information about an app. This dictionary is returned by active Application and launched Applications, and is also provided in the user Info of NSWorkspace notifications for app launch and termination. Note that these constants are considered legacy. Swift Playgrounds is a dedicated app for iPad specially designed to make learning and experimenting with Swift coding fun. Its Learn to Code lessons are presented in the form of interactive puzzles, with different additional challenges that let you explore and master coding with the Swift language. Swift: How to exit a macOS app with an exit code. Ask Question Asked 3 years, 7 months ago. Active 3 years, 7 months ago. Viewed 2k times 0. I use a.app to bundle my command line tool (I use a framework). To quit the app, I use NSapp.terminate(self) and catch it: func applicationWillTerminate( aNotification: Notification) if exitSuccessful.
How to force an app to quit
Press these three keys together: Option, Command, and Esc (Escape). Or choose Force Quit from the Apple menu in the upper-left corner of your screen. (This is similar to pressing Control-Alt-Delete on a PC.)
Then select the app in the Force Quit window and click Force Quit.
You can even force the Finder to quit, if it stops responding: Select Finder in the Force Quit window, then click Relaunch.
![App App](/uploads/1/3/4/2/134261833/294699496.png)
Learn more
How to force an app to close on your iPhone, iPad, or iPod touch.
While it’s not accurate to say that Swift is “Objective-C without the C”, it’s for lack of resemblance to Objective-C, not the absence of C. Swift is vehementlynot C.
Swift certainly draws inspiration from Haskell, Rust, Python, D, and other modern languages, but one can perhaps best understand the language as a rejection of everything that’s broken in C:
- C is unsafe by default. Swift is safe by default (hence the
unsafe
naming of pointer manipulation functions). - C has undefined behavior. Swift has well-defined behavior.
- C uses preprocessor directives capable of unspeakable evil. Swift has a safe subset of preprocessor directives.
One could go as far to say that Swift’s type system was specifically designed out of spite for C++.
In Objective-C, checking for the availability of an API was accomplished through a combination of C preprocessor directives, conditionals on
class
, respondsToSelector:
, and instancesRespondToSelector:
:However, as noted previously, Swift’s compiler directives are extremely constrained, allowing only for compiler flags and conditional compilation against specific operating systems, architectures, and language versions:
Swift Exit()
Function | Valid Arguments |
---|---|
os() | macOS , iOS , watchOS , tvOS , Linux |
arch() | x86_64 , arm , arm64 , i386 |
swift() | >= followed by a version number |
Unfortunately,
os()
doesn’t offer any insight into the specific version of macOS or iOS, which means that checks must be made at runtime. And with Swift’s less-forgiving treatment of nil
, checking for constants Objective-C-style results in a crash.So how do you check the system version in Swift to determine API availability? Read on to find out.
ProcessInfo
Anticipating the need for a Swift-friendly API for determining API version at runtime, iOS 8 introduces the
operatingSystemVersion
property and isOperatingSystemAtLeast
method on ProcessInfo
. Both APIs use a new OperatingSystemVersion
value type, which contains the majorVersion
, minorVersion
, and patchVersion
.Apple software releases follow semantic versioning conventions.
isOperatingSystemAtLeast
For a simple check, like “is this app running on iOS 9?”,
isOperatingSystemAtLeast
is the most straightforward approach.operatingSystemVersion
For more involved version comparison, the
operatingSystemVersion
can be inspected directly. Combine this with Swift pattern matching and switch
statements for syntactic concision:UIDevice systemVersion
![App App](/uploads/1/3/4/2/134261833/121452296.png)
As an alternative, one can use the
systemVersion
property UIDevice
:Use
String.CompareOptions.numeric
when comparing version number strings to ensure that, for example, '2.5' < '2.10'
.String comparison and
ComparisonResult
aren’t as sexy as a dedicated value type like OperatingSystemVersion
, but it gets the job done all the same.NSAppKitVersion
Another approach to determining API availability is to check framework version numbers. Unfortunately, Foundation’s
NSFoundationVersionNumber
and Core Foundation’s kCFCoreFoundationVersionNumber
have historically been out of date, missing constants for past OS releases.This is a dead-end for iOS, but macOS can pretty reliably check against the version of AppKit, with
NSAppKitVersion
:Macos App Download
If you pair this with an extension to make
NSAppKitVersion
conform to Comparable
, you can remove the .rawValue
s as well.To summarize, here’s what you need to know about checking the system version in Swift:
Swift Exit App Macos Download
- Use
#if os(iOS)
preprocessor directives to distinguish between iOS (UIKit) and macOS (AppKit) targets. - For minimum deployment targets of iOS 8.0 or above, use
ProcessInfo
operatingSystemVersion
orisOperatingSystemAtLeast
. - For minimum deployment targets of iOS 7.1 or below, use
compare
withString.CompareOptions.numeric
onUIDevice
systemVersion
. - For macOS deployment targets, compare
NSAppKitVersion
against available AppKit constants.