March 2017
Intermediate to advanced
226 pages
5h 13m
English
Both if-case and guard-case are most valuable when used to bind associated values to variables. They allow you to reach inside an enumeration and access those values based on an enumeration’s specific cases:
| | enum Result<T> { case success(T), error(Error) } |
| | guard case .success(let value) = result else { return } // yes |
| | print(value) |
When pattern matching, prefer the ~= operator to if-case and guard-case. The following statements perform identical tests, checking whether myValue falls within a range:
| | if range ~= myValue { print("success") } // yes |
| | if case range = myValue { print("success") } // no |
Of these, the first is readable and simple. The second, while logically identical, is ...
Read now
Unlock full access