KeyPaths are a pretty popular Swift syntax!

But do you think you know all the features of KeyPaths? 🤨

Here are 5 things you (probably) didn't know about KeyPaths 🧵
Let’s start with #1️⃣: Whenever you use a method like `.map { }` or .filter { }`, you can pass it a KeyPath instead of a closure!

And the compiler will take care of automatically translating that KeyPath to a closure 👌
Moving on to #2️⃣: Did you know there’s actually more than one type of KeyPath?

The classic `KeyPath` type is actually a reference to a read-only property.

When we take a `KeyPath` to a writable property, we get a `WritableKeyPath`:
And when working with a reference type, we even get a `ReferenceWritableKeyPath`!
Most of the time, we don’t need to care about these different types, as the compiler will map a literal KeyPath to the correct type.
However, if we write our own APIs then these differences become important, because we’ll need to use the correct type depending on what we want to do with the KeyPath.

(And there are even a few additional types to deal with type erasure!)
Let’s move on to #3️⃣: We are all familiar with KeyPaths that reference properties, but did you know that a KeyPath can also reference a subscript?
Here, for instance, I’m taking a KeyPath to the property `name` of the second element in an array of `Person`.

From there we can simply invoke that KeyPath over the array just like we would do with a single value 👍
Now for #4️⃣, let’s talk about how we can use KeyPaths to write nice little DSLs, like this type-safe predicate syntax.

To make this syntax work in Swift, we actually only need to implement a single function!
We just need to implement an overload of the operator `>`, that takes as its left hand side a KeyPath and as its right hand side a constant, and then use the KeyPath to compare the value of the property to that of the constant!
Finally for #5️⃣: Did you know we can use dynamic member lookup with a KeyPath?

Consider the struct `Order` I’ve just declared: I would like to be able to access the properties of the `address` directly on the struct itself:
To do so, we start by annotating the struct with @dynamicMemberLookup.

Then, we implement a subscript that takes as its argument a KeyPath to a property of an Address and we invoke that KeyPath on the property `address`:
And that’s it, we can now directly access the properties of the `address`!
Since our implementation of the subscript relies on a KeyPath, this syntax is type-safe: meaning that if we try to call a property that doesn’t exist on an `address`, it will result in a compilation error 👌
That's all for this thread 🧵, these were the 5 things you (probably) didn’t know about KeyPaths!

Feel free to share the love and RT the original tweet 🥰

Don't miss my next thread and follow me @v_pradeilles

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Vincent Pradeilles

Vincent Pradeilles Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @v_pradeilles

Dec 5
You’ve heard that actors are a very powerful feature of Swift, but you're not really sure how they work? 🤨

Don’t worry! I’ve got you covered! 😌

In just a few tweets we’ll go over everything you need to understand how to use an actor in Swift! 🧵 Image
Let’s start with this class that implements a simple cache of images: Image
As long as all our call sites are happening on a single thread of execution, everything is working fine 👌
Read 13 tweets
Nov 21
You’ve heard that async / await is amazing with asynchronous code, but you're not really sure how it works? 🤨

Don’t worry! I’ve got you covered! 😌

In just a few tweets we’ll go over everything you need to know to understand how this powerful tool works! 🧵 Image
Let’s start by talking about functions!

In Swift there are two kind of functions.

First, we have the functions that don’t need to wait on anything to compute their results:
Even though these functions can take a long time to compute their result, they’re able to compute it without needing to take a pause while they execute.

We call these functions **synchronous** functions:
Read 11 tweets
Sep 19
MVVM can feel a bit mysterious when you’ve never used it 😶‍🌫️

But I swear it’s really simple to understand! 😌

In just a few tweets we’ll go over everything you need to understand how this architecture works! 🧵 Image
Let’s start with this ViewController.

If you think about it, this ViewController actually has a lot of responsibilities!

It must: fetch its data, format its data and then finally display its data 💪 Image
With so many responsibilities, this ViewController will become harder and harder to maintain as our app will grow in complexity 😰

That’s why it makes sense to extract some of these responsibilities to another object called a ViewModel!

So let’s do it in 6 easy steps 🚀
Read 11 tweets
Jan 19, 2019
My second talk at @iosconfsg on behalf of @worldlinexpert was all about how Swift KeyPaths can be leveraged to build some pretty effective syntax and patterns. Here's a thread that recaps some of the key ideas ⤵️
Swift 4 has introduced a new construct called KeyPaths, which allow to defer calls to the getter or setter of a property. They essentially perform the same job than a closure would, but with less $0 hanging around!
While they don't look like much by themselves, I want to show you that with a little bit of extra code, KeyPaths can be used to unlock some very nice and clean syntax!
Read 10 tweets
Jan 16, 2019
In a few hours I’m going to take-off ✈️ and head towards Singapore 🇸🇬 to attend @iosconfsg on behalf of @worldlinexpert 🤩
I plan on sharing pics 📸 and video 🎥 updates the whole time I’m away!
I’ll post everything right below this tweet ⤵️
First step of the trip: let’s ride the (very expensive 😱) shuttle, heading to the airport!
Now since I won’t realistically get any food on the plane before 11pm, I’m going to stack up energy on some good old junk food 🍔🥤
Read 37 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(