March 2017
Intermediate to advanced
226 pages
5h 13m
English
Ternary operators enable you to operate on three targets. Like most programming languages, Swift implements a single ternary operator to express a conditional choice. You state a condition and select a value based on its truth value. The Swift version is essentially identical to those you’d find in C and Objective-C, among other languages:
| | let result = condition ? valueIfTrue : valueIfFalse |
The common-sense rules of ternary layout are these:
Keep it simple. Ternary statements are best for uncomplicated evaluation, especially when the corresponding if statement requires additional lines without adding clarity or safety.
Use a single line for simple values. Prefer return value > 0 ? 1 : -1 to a multiline version of the ...
Read now
Unlock full access