Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seeking an "exit" equivalent in Swift [duplicate]

Tags:

swift

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?

like image 520
pjs Avatar asked Jun 08 '14 01:06

pjs


People also ask

What does exit () do in Swift?

exit() Terminates the current thread.

How do you end a swift code?

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.

How do you exit an if statement in Swift?

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!


1 Answers

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) 
like image 98
rob mayoff Avatar answered Oct 25 '22 14:10

rob mayoff