Optional pattern

Speaking of optionals, there is also an optional pattern. The optional pattern consists of an identifier pattern followed immediately by a question mark. You can use this pattern in the same places you would use enumeration case patterns.

You can rewrite the solution to the mini exercise as:

for case let name? in names { print(name) // 4 times

}

Optional patterns are syntactic sugar for enumeration case patterns containing optional values. Syntactic sugar simply means a nicer way of writing the same thing.

results matching ""

    No results matching ""