Chapter    16

Ternary Conditional Operator

You use the ternary conditional operator to evaluate a question and then do one of two things based on the result of the question. The ternary conditional operator is written like this: question ? action1 : action2 (see Listing 16-1).

The question is an expression that returns a boolean true or false. If the question returns true, then the first action takes place. If the question returns false, then the second action takes place.

Listing 16-1. Ternary Conditional Operator

let a = 5a == 5 ? "We're good" : "O0ps, not quite"

In Listing 16-1, the first part of the ternary conditional operator is the statement a ==5, which is using the equality comparison operator to test to see whether a is equal to 5. ...

Get Swift Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.