A few weeks ago I came across a situation where I needed to change a collection type from a Swift Dictionary to an Array. Then when using the subscript methods to access the values I noticed the Dictionary returned an optional and the Array returned a non-optional. My first thoughts were “That can’t be right. Surely an Array subscript should return an optional too.” Then I thought maybe I could extend Array to return an optional from a subscript or something similar but it seemed like an unlikely oversight for a new, well thought out language like Swift.

At first glance this looks strange because both have identical syntax and therefore we expect both to have identical behaviour. Then looking in the Swift standard library the differences are clearly there. “Value?” for Dictionary and “Element” for Array. …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store