March 2017
Intermediate to advanced
226 pages
5h 13m
English
When looping over a collection whose components are optionals, prefer case let and optional sugar (the question mark) to conditional binding. These approaches save you a scope and offers top-level access to each non-nil item. The following example shows the simplicity you gain by adopting optional sugar:
| | for case let item? in array { // yes |
| | // iterates through each non-nil item in array |
| | ... |
| | } |
| | |
| | for item in array { // no |
| | if let item = item { |
| | // iterates through each non-nil item in array |
| | ... |
| | } |
| | } |
When working with optional elements, apply flatMap to filter a collection. This approach converts the following array of optionals into a new collection of non-optional elements. ...
Read now
Unlock full access