Understanding Swift KeyPaths

preview_player
Показать описание
In this video, I want to introduce you to KeyPaths and how you can use them in your Swift projects.

We will start by looking a what a KeyPath is and how you can use them to reference to a property of an object or a struct and then look at some examples of how you might apply them in your code.

This will include using keyPaths for sorting and mapping arrays, we will look at the DynamicMemberLookup attribute and two very practical examples for using KeyPaths to simplify your code

We will finish with a short explanation of how keyPaths are used in Lists for SwiftUI

If you want to support my work, you can buy me a coffee :)

Table of Contents:
0:00 Introduction
1:14 The Basics
5:40 KeyPathComparator
8:22 KeyPaths for Mapping
9:46 DynamicMemberLookup
15:50 Practical Examples
22:52 KeyPaths in Swift Lists

Starter Project Files:
(If you are having problems downloading zip files, use Safari. Chrome restricts zip files by default)

Completed Project:

References mentioned in video:
What is Swift KeyPath in Swift
How to Use DynamicMemberLookup in Swift
Higher order Function Videos
map, compactMap, flatMap
contains, forEach, Sorted
reduce, filter, removeAll

Other References:
Stewart Lynch's YouTube Channel:
Stewart Lynch on Mastodon:
Stewart Lynch on Twitter:
Stewart Lynch on LinkedIn
Рекомендации по теме
Комментарии
Автор

Thank you for your dedication to providing excellent content with great clarity. I still have not clearly seen the benefit of keyPath. I have tried to use “dot notation” approach to the two practical examples and achieved the same results as with keyPath’s approach. I am sure that it is powerful but with my current limited knowledge unfortunately, I have not been able to appreciate the significant benefits of keyPath yet. 😢 I hope I soon will.

TuanHoang-qjyq
Автор

The content is always great as usual. Thank you for all your work and sharing these.

CarterTsai-pt
Автор

Thanks again! I've needed this one (and one on subscripts, so ditto on the request for that) for a long time.

robertdavis
Автор

Great examples, thank you so much for your work 🙂

andresraigoza
Автор

Great video. Learned some new techniques. Thank you.

parietal
Автор

Awesome! Love the format of these videos! Thank you 😊

hunterdobb
Автор

Thanks a lot for SwiftUI’s examples 💫 please keep them up 🎈

Mahadev-xu
Автор

Thanks a lot Mr Stewart! This series is really helpful. Any chance you'd cover Swift subscripts in one of the videos? I actually requested you cover Swift KeyPaths when you asked on Twitter, so thanks for this!

femialiu
Автор

An extremely useful video. Thanks to it I (and some of your other videos, ie CoreData) I was able to figure out and now share a use case for key paths. Suppose you want a picker that will then display one total for any number of parameters in a CoreData entity. Populating an array with a struct that included a name and a keypath of type “ReferenceWritableKeyPath<Entity, Double> did the job. The structure name is used for the picker and the tag links it to variable that is used for the reducing function (in my case a static function locate in the core data properties extension. Keypaths add a whole new dimension to data manipulation where parameters can now be treated as data as well. Perhaps there is another way to do this as well. I did try use an enum and associated value constructor and a static var all cases but this did not work for me.

wallacewittkoff
Автор

wow, such a great job, thanks! looks like way safer js))))

РоманРоман-нн
Автор

Great as always my question do u have any database course Tutorial to buy or watch? Thx sir

kadeus
Автор

Great content except example of wine.. as for me

shurale