| Start |
|---|
| Optional binding and "where" clauses Optionals (opens new window) must be unwrapped before they can be used in most expressions. You can reuse the same name for the newly bound variable, shadowing the original: Combine multiple optional bindings with commas ( Apply further constraints after the optional binding using a If you're feeling adventurous, interleave any number of optional bindings and In Swift 3, |
| Using Guard Guard checks for a condition, and if it is false, it enters the branch. Guard check branches must leave its enclosing block either via Using guards can help keep nesting levels low (opens new window), which usually improves the readability of the code. Guard can also check if there is a value in an optional (opens new window), and then unwrap it in the outer scope: Guard can combine optional (opens new window) unwrapping and condition check using |
| Basic conditionals: if-statements An
Basic operators like The logical AND operatorIf The logical OR operatorIf The logical NOT operator |
| Ternary operator Conditions may also be evaluated in a single line using the ternary operator: If you wanted to determine the minimum and maximum of two variables, you could use if statements, like so: The ternary conditional operator takes a condition and returns one of two values, depending on whether the condition was true or false. The syntax is as follows: This is equivalent of having the expression: The above code can be rewritten using ternary conditional operator as below: In the first example, the condition is a < b. If this is true, the result assigned back to min will be of a; if it's false, the result will be the value of b. Note: Because finding the greater or smaller of two numbers is such a common operation, the Swift standard library provides two functions for this purpose: max and min. |
| Nil-Coalescing Operator The nil-coalescing operator The nil-coalescing operator is shorthand for the code below that uses a ternary operator: this can be verified by the code below: Time For An Example RemarksFor more information about conditional statements, see The Swift Programming Language (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference