Understand the protocols that make Arrays, Dictionaries, and Sets work

a newly hatched Monarch butterfly and two pupae at different stages of maturity
a newly hatched Monarch butterfly and two pupae at different stages of maturity
Photo by Suzanne D. Williams on Unsplash

The three primary collection types in Swift — Array, Dictionary and Set — need no introduction. But if you’ve ever explored protocol conformance hierarchy for any of them, you may have noticed that it may be quite long and seemingly overcomplicated. For example, for Array, it may look like following (due to multiple protocol conformances, there can be multiple paths, and this is not the longest one):

Sequence <- Collection <- MutableCollection <- Array

Image for post
Image for post
Photo by Stefan Steinbauer on Unsplash

Many programming languages have a shortcut syntax of square brackets to directly access the value in array or dictionary. In Swift this mechanism is more flexible and generic and is called subscripts. You can create custom subscripts for any type, using indices of any type and arbitrary amount. This article though is not about subscripts in general, but about one somewhat opaque detail of its behaviour I’ve come across and wanted to share.

Assume you have an array of strings, and suddenly you realize that one of the values in it isn’t exactly what you need, so you have to adjust it. …

Image for post
Image for post
Photo by Shane Aldendorff on Unsplash

Welcome back to Swift Property Wrappers exploration! In my previous article we’ve started talking about its internal implementation and tried to grasp the intuition behind this feature. Just to recap, we have figured out that the property wrapper (PW) is basically a regular struct, instance of which invisibly substitutes the instance of other properties (wrapped properties) in any other struct or class. As an example we’ve introduced the struct State (the PW-struct) that prints out its only property wrappedValue once it is set, and we are using it with properties of Dashboard struct.

Get to know one of the latest major features of Swift

Image for post
Image for post
Photo by Henry Ascroft on Unsplash

It’s been some time since Swift acquired a pretty interesting feature called property wrappers. This feature is reputed to be designed mostly to facilitate SwiftUI and Combine API, so we (modern Swift developers ) had to face it immediately, with no time to start slowly and explore its guts.

But to be honest: Do all of us have a solid and deep understanding of how this feature is actually implemented and how it works?

For example, what makes it possible to set the @State-marked property of a let constant (or of immutable self) without getting a Cannot assign to property error? …


Valeriy Bragin

Swift enthusiast

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