O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter    12

Assertions

Assertions are a tool that you can use to help with your debugging efforts. Assertions work by testing a condition to see whether the condition is satisfied. If the condition is not satisfied, then a runtime error occurs, and the program stops executing. Assertions may have an optional message that is printed in the console window when the condition is not met.

To create an assertion, write the assertion shown in Listing 12-1.

Listing 12-1. Assert

var triangleSides = 4assert(triangleSides == 3, "A triangle must have three sides")

The assert statement in Listing 12-1 is testing to make sure that the triangleSides variable has the correct number of sides. If triangleSides has three sides, nothing happens. If triangleSides ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required