Swift Essentials
上QQ阅读APP看书,第一时间看更新

Conditional logic

There are two key types of conditional logic in Swift (known as branch statements in the grammar): the if statement and the switch statement. Unlike other languages, the body of the if must be surrounded with braces {}, and if typed in at the interpreter, the opening brace { must be on the same line as the if statement. The literal values true and false can be used as well as other boolean expressions.

If statements

Conditionally unpacking an optional value is so common that a specific Swift pattern has been created to avoid evaluating the expression twice:

var shopping = [ "Milk", "Eggs", "Coffee", "Tea", ]
var costs = [ "Milk":1, "Eggs":2, "Coffee":3, "Tea":4, ]
var cost = 0
if let cm = costs["Milk"] {
.   cost += cm
. }
> cost
$R0: Int = 1

The if block only executes if the optional value exists. The definition of the constant cm only exists for the body of the if block, and does not exist outside that scope. Furthermore, cm is a non-optional type, so it is guaranteed to not be nil.

To execute an alternative block if the item cannot be found, an else block can be used:

> if let cb = costs["Bread"] {
.  cost += cb
. } else {
.  println("Cannot find any Bread")
. }
Cannot find any Bread

Other boolean expressions can include any expression that conforms to the BooleanType protocol, the equality operators == and !=, the identity operators === and !==, as well as the comparison operators <, <=, >, >=. The is type operator provides a test to see whether an element is of a particular type.

Note

The difference between the equality operator and the identity operator is relevant for classes or other reference types. The equality operator asks "Are these two values equivalent to each other?" whereas the identity operator asks "Are these two references equal to each other?"

There is a boolean operator specific to Swift, which is the ~= pattern match operator. Despite the name, this isn't anything to do with regular expressions; rather, it's a way of asking whether a pattern matches a particular value. This is used in the implementation of the switch block, which is covered in the next section.

In addition to the if statement, there is a ternary if expression similar to other languages. After a condition, a question mark (?) is used, followed by an expression to be used if the condition is true, then a colon (:) followed by the false expression:

> var i = 17
i: Int = 17
> i % 2 == 0 ? "Even" : "Odd"
$R0: String = "Odd"

Switch statements

In addition to if/else, Swift also has a switch statement, similar to C and Java's switch. However, it differs in two important ways. Firstly, case statements no longer have a default fall-through behavior (so there are no bugs introduced by missing a break statement) and secondly, the value of the case statements can be expressions instead of values, pattern matching on type and range. At the end of the corresponding case, the evaluation jumps to the end of the switch block, unless the fallthrough keyword is used. If no case statements match, the default statement is executed:

> var position = 21
position: Int = 21
> switch position {
.   case 1: println("First")
.   case 2: println("Second")
.   case 3: println("Third")
.   case 4...20: println("\(position)th")
.   case position where (position % 10) == 1:
.     println("\(position)st")
.   case let p where (p % 10) == 2:
.     println("\(p)nd")
.   case let p where (p % 10) == 3:
.     println("\(p)rd")
.   default: println("\(position)th")
. }
21st

In the preceding example, the expression prints out First, Second, or Third if the position is 1, 2, or 3 respectively. For numbers between 4 and 20 (inclusive), it prints out the position with a th ordinal. Otherwise, for numbers that end with 1, it prints st; for numbers that end with 2, it prints nd; and for numbers that end with 3, it prints rd. For all other numbers, it prints th.

The 4...20 range expression in a case statement represents a pattern. If the value of the expression matches that pattern, then the corresponding statements will be executed:

> 4...10 ~= 4
$R0: Bool = true
> 4...10 ~= 21
$R1: Bool = false

There are two range operators in Swift: an inclusive or closed range, and an exclusive or half-open range. The closed range is specified with three dots; 1...12 will give a list of integers between one and twelve. The half-open range is specified with two dots and a less than operator; so 1..<10 will provide integers from 1 to 9 but exclude 10.

The where clause in the switch block allows an arbitrary expression to be evaluated, provided that the pattern matches. These are evaluated in-order, in the sequence they are in the source file. If a where clause evaluates to true, then the corresponding set of statements will be executed.

The let variable syntax can be used to define a constant that refers to the value in the switch block. This local constant can be used in the where clause or the corresponding statements for that specific case. Alternatively, variables can be used from the surrounding scope.

Note

If multiple case statements need to match the same pattern, they can be separated with commas in the form of an expression list. Alternatively, the fallthrough keyword can be used to allow the same implementation to be used for multiple case statements.