This is the Optional Pattern. It tests and unwraps an Optional
, executing the conditional only if the Optional
is non-nil.
The keyword case
is needed because it follows from the original switch
...case
syntax. The case
tests a pattern and if it matches then the following statement is executed. In your example the let next?
is the pattern. If the value is unwrapped and assigned then the case
matches and your code is executed.
From the documentation:
Optional Pattern
An optional pattern matches values wrapped in a Some(Wrapped) case of
an Optional or ImplicitlyUnwrappedOptional
enumeration. Optional patterns consist of an identifier pattern
followed immediately by a question mark and appear in the same places
as enumeration case patterns.
Because optional patterns are syntactic sugar for Optional and
ImplicitlyUnwrappedOptional enumeration case patterns, the following
are equivalent:
let someOptional: Int? = 42
// Match using an enumeration case pattern
if case .Some(let x) = someOptional {
print(x)
}
// Match using an optional pattern
if case let x? = someOptional {
print(x)
}
The optional pattern provides a convenient way to iterate over an
array of optional values in a for-in statement, executing the body of
the loop only for non-nil elements.
let arrayOfOptionalInts: [Int?] = [nil, 2, 3, nil, 5]
// Match only non-nil values
for case let number? in arrayOfOptionalInts {
print("Found a \(number)")
}
// Found a 2
// Found a 3
// Found a 5