What are Swift’s Keypaths?
They help with KVO and more!
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 11.4.1, and Swift 5.2.2
Prerequisites
You will be expected to be aware how to make a Single View Application, or a Playground to run Swift code
Terminology
keypath: Read-only access to a property
writablekeypath: read-write access to a value-type property
referencewritablekeypath: read-write access to a reference-type property
Motivation
Combine
uses ReferenceWritableKeyPath<Root, Bool>
in the assign function
which is the one that allows us to connect to UIKit
elements, for example
That seems fine, but what are keypaths? How are they used, and what do they mean?
Could you make an article explaining this for me?
I don’t mind if I do!
What is a keypath?
A keypath provides read-only access to a property, whilst a writable keypath provides (well…) writable access to a property.