Chapter 19.3 Keypaths

This section explains how to use keypaths, but not why we’d want to or how they are useful. Would be nice to add this.

I can’t speak for the authors, but having experience in Keypaths I feel like the answers to your questions are implied in the examples: they store references to properties and you can use the reference instead of the actual path where you need it.

let discountTotalPath = \
let discountTotal1 = reservation[0][keyPath: discountTotalPath]
let discountTotal2 = reservation[1][keyPath: discountTotalPath]

The above is just mechanics, but because you can pass a Keypath as a parameter, function, look them up, append, etc. Then you can do lookups into your data structures easily. You see this a lot in SwiftUI.

This topic was automatically closed after 166 days. New replies are no longer allowed.