I ran into this code which is part of a Swift implementation of a linked list in the Swift Algorithm Club. Throughout the implementation the author uses case let
fo
Swift 2 took the pattern paradigm from switch/case
statements and allowed it to be used in other contexts (if
, while
and so on).
So now, rather than just simple comparisons, you can use these pattern matching comparisons in conditionals as well.
As one example, rather than:
if (a >= 0) and (a <= 255)
you can instead use:
if case 0...255 = a
That's a trivial example but it can become much more useful once you realise the rather large number of pattern matching options available to you.
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