assert(_:_:file:line:)
Performs a traditional C-style assert with an optional message.
Declaration
func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line)Parameters
- condition:
The condition to test.
conditionis only evaluated in playgrounds and-Ononebuilds. - message:
A string to print if
conditionis evaluated tofalse. The default is an empty string. - file:
The file name to print with
messageif the assertion fails. The default is the file whereassert(_:_:file:line:)is called. - line:
The line number to print along with
messageif the assertion fails. The default is the line number whereassert(_:_:file:line:)is called.
Discussion
Use this function for internal consistency checks that are active during testing but do not impact performance of shipping code. To check for invalid usage in Release builds, see precondition(_:_:file:line:).
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),conditionis not evaluated, and there are no effects.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.