Checks a necessary condition for making forward progress.
SDK
- Xcode 6.1+
Framework
- Swift Standard Library
Declaration
Parameters
condition
The condition to test.
condition
is not evaluated in-Ounchecked
builds.message
A string to print if
condition
is evaluated tofalse
in a playground or-Onone
build. The default is an empty string.file
The file name to print with
message
if the precondition fails. The default is the file whereprecondition(_:
is called._: file: line:) line
The line number to print along with
message
if the assertion fails. The default is the line number whereprecondition(_:
is called._: file: line:)
Discussion
Use this function to detect conditions that must prevent the program from proceeding, even in shipping code.
In playgrounds and
-Onone
builds (the default for Xcode’s Debug configuration): Ifcondition
evaluates tofalse
, stop program execution in a debuggable state after printingmessage
.In
-O
builds (the default for Xcode’s Release configuration): Ifcondition
evaluates tofalse
, stop program execution.In
-Ounchecked
builds,condition
is not evaluated, but the optimizer may assume that it always evaluates totrue
. Failure to satisfy that assumption is a serious programming error.