March 2017
Intermediate to advanced
226 pages
5h 13m
English
Optional collections are common when working with system calls and web service results. When a function call returns an optional collection (not a collection of optionals but an optional return type), you may consider any of several iteration approaches. These include nil coalescing (the simplest solution), functional chaining, and nested conditional binding.
nil coalescing avoids conditional binding overhead and the result is always well behaved. It provides a simple and reliable way to coerce an optional collection to a non-optional alternative, providing a safe fallback for nil results:
| | // Nil-coalescing removes the need to unwrap |
| | for item in (items ?? []) { |
| | print(item) |
| | } |
Note that ...
Read now
Unlock full access