I tried putting exit(0)
in a small test (command-line) program. Xcode gave an error message stating Use of unresolved identifier 'exit'
. This was confusing to me, since this question seemed to show exit()
works. After combing through "The Swift Programming Language" I couldn't find any mention of an exit
keyword/command. So how do you explicitly halt execution in Swift?
exit() Terminates the current thread.
exit() is for normal termination, and allows you to provide a return code. abort() is for when something has gone wrong. It's basically forcing a crash.
In Swift, the break statement is used to immediately end the execution of a loop, a switch statement, an if statement of a do statement. As such, the break statement consists of a single break keyword followed optionally by a (you guessed it) statement label!
The exit
function isn't a keyword or built-in in C or Objective-C either. It's a library function. In Swift, you can access it by importing Darwin
:
import Darwin exit(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With