Assertions and Preconditions: Enforcing Preconditions

Use a precondition whenever a condition has the potential to be false, but must definitely be true for your code to continue execution.

// In the implementation of a subscript...
precondition(index > 0, "Index must be greater than zero.")

Additional information

  • You write a precondition by calling the precondition(_:_:file:line:) function. You pass this function an expression that evaluates to true or false and a message to display if the result of the condition is false.

Assertions and Preconditions: Enforcing Preconditions — Structure map

Clickable & Draggable!

Assertions and Preconditions: Enforcing Preconditions — Related pages: