precondition(_:_:file:line:)
Checks a necessary condition for making forward progress.
Declaration
func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line)Parameters
- condition:
The condition to test.
conditionis not evaluated in-Ouncheckedbuilds. - message:
A string to print if
conditionis evaluated tofalsein a playground or-Ononebuild. The default is an empty string. - file:
The file name to print with
messageif the precondition fails. The default is the file whereprecondition(_:_:file:line:)is called. - line:
The line number to print along with
messageif the assertion fails. The default is the line number whereprecondition(_:_:file:line:)is called.
Discussion
Use this function to detect conditions that must prevent the program from proceeding, even in shipping code.
In playgrounds and
-Ononebuilds (the default for Xcode’s Debug configuration): Ifconditionevaluates tofalse, stop program execution in a debuggable state after printingmessage.In
-Obuilds (the default for Xcode’s Release configuration): Ifconditionevaluates tofalse, stop program execution.In
-Ouncheckedbuilds,conditionis not evaluated, but the optimizer may assume that it always evaluates totrue. Failure to satisfy that assumption is a serious programming error.