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
Most of the methods and properties available for Array
are spread out over those protocols. But what is the point of having so many stages of ripening of Array
’s functionality? …
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. …
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.
Any struct is eligible to be a PW-struct as long as it has the stored property with the exact name wrappedValue
. You mark it with @propertyWrapper
attribute, and now the name of this struct itself is turned to a brand new attribute (in our example — @Printed
), which you use to mark stored properties of any other structs and classes, as long as type of this property is the same as type of wrappedValue
in PW-struct. So what effect does this attribute cause? This might appear a bit tangled at first sight, but consider this way of reading the code with PWs: just keep in mind that there is no longer a stored Double
value of altitude
, but instead you have another property of type Printed
(it is indeed there, it’s implicitly synthesized by compiler, but still explicitly accessible as _altitude
), and your original altitude
is now nothing more than a shortcut way of accessing _altitude.wrappedValue
. …
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? …
About