# \#keypaths

**URL:** https://forums.swift.org/tag/keypaths/56.md

[Latest](https://forums.swift.org/latest.md) · [Categories](https://forums.swift.org/categories.md) · [Tags](https://forums.swift.org/tags.md)

---

## [MainActor + KeyPathComparator + class = error](https://forums.swift.org/t/mainactor-keypathcomparator-class-error/85447)

<div class="topic-metadata">

**Author:** [@marco.masser](https://forums.swift.org/u/marco.masser)\
**Replies:** 10\
**Last updated:** [March 24, 2026, 4:30pm UTC](https://forums.swift.org/t/mainactor-keypathcomparator-class-error/85447 "2026-03-24T16:30:14Z")

</div>

I’m running into a situation where the compiler shows an error and I’m not quite sure if it is my code or the compiler that is incorrect. Any input would be greatly appreciated! My project uses Swift 6, MainActor defaul…

---

## [@dynamicMemberLookup + nested KeyPath access not triggering my custom observation access tracking](https://forums.swift.org/t/dynamicmemberlookup-nested-keypath-access-not-triggering-my-custom-observation-access-tracking/84165)

<div class="topic-metadata">

**Author:** [@Blacksmith](https://forums.swift.org/u/Blacksmith)\
**Replies:** 1\
**Last updated:** [January 15, 2026, 8:44pm UTC](https://forums.swift.org/t/dynamicmemberlookup-nested-keypath-access-not-triggering-my-custom-observation-access-tracking/84165 "2026-01-15T20:44:12Z")

</div>

Hello everyone, I’m experimenting with a custom observation mechanism for value-type State (similar in spirit to @Observable, but without macros). While doing that, I ran into an unexpected behavior involving @dynamicMe…

---

## [Enumerating all property key paths in class and superclasses](https://forums.swift.org/t/enumerating-all-property-key-paths-in-class-and-superclasses/81157)

<div class="topic-metadata">

**Author:** [@lukeh](https://forums.swift.org/u/lukeh)\
**Replies:** 2\
**Last updated:** [July 19, 2025, 3:18am UTC](https://forums.swift.org/t/enumerating-all-property-key-paths-in-class-and-superclasses/81157 "2025-07-19T03:18:50Z")

</div>

I've been using a fork of @philipturner's swift-reflection-mirror in SwiftOCA to enumerate all property key paths in a class and its superclasses. Obviously, no expectation this was ever supported or guaranteed to not br…

---

## [Crash creating key path to variadic property](https://forums.swift.org/t/crash-creating-key-path-to-variadic-property/80671)

<div class="topic-metadata">

**Author:** [@George](https://forums.swift.org/u/George)\
**Replies:** 1\
**Last updated:** [June 25, 2025, 11:32pm UTC](https://forums.swift.org/t/crash-creating-key-path-to-variadic-property/80671 "2025-06-25T23:32:27Z")

</div>

The following code consistently crashes for me when creating the key path on both 6.1 and the 6.2 betas: struct Foo\<each T\> { struct Bar { let bar: (repeat each T) } func bar() { // Crashes here …

---

## [SE-0479: Method and Initializer Key Paths](https://forums.swift.org/t/se-0479-method-and-initializer-key-paths/79457)

<div class="topic-metadata">

**Author:** [@beccadax](https://forums.swift.org/u/beccadax)\
**Replies:** 26\
**Last updated:** [June 3, 2025, 9:37am UTC](https://forums.swift.org/t/se-0479-method-and-initializer-key-paths/79457 "2025-06-03T09:37:07Z")

</div>

Hello Swift community, The review of "Method and Initializer Key Paths" begins now and runs through May 5, 2025. The proposal is available here: swift-evolution/proposals/0479-method-and-initializer-keypaths.md at mai…

---

## [KeyPath + String](https://forums.swift.org/t/keypath-string/77732)

<div class="topic-metadata">

**Author:** [@bdkjones](https://forums.swift.org/u/bdkjones)\
**Replies:** 20\
**Last updated:** [May 6, 2025, 4:23pm UTC](https://forums.swift.org/t/keypath-string/77732 "2025-05-06T16:23:42Z")

</div>

TLDR Swift should add first-class support for string-ifying KeyPath. I realize that you moved heaven and earth to keep KeyPath and String from EVER knowing about each other, but that has forced everyone to jump through l…

---

## [\[Pitch\] Method Key Paths](https://forums.swift.org/t/pitch-method-key-paths/76678)

<div class="topic-metadata">

**Author:** [@amritpan](https://forums.swift.org/u/amritpan)\
**Replies:** 33\
**Last updated:** [March 27, 2025, 2:13am UTC](https://forums.swift.org/t/pitch-method-key-paths/76678 "2025-03-27T02:13:06Z")

</div>

Hello everyone, The following is a pitch for method key paths in the Swift language. We welcome any feedback or questions about this pitch! \[Pitch\] Method Key Paths Proposal: SE-NNNN Authors: Amritpan Kaur, Pavel Yas…

---

## [KeyPath Value is Nil, but If-Let succeeds?](https://forums.swift.org/t/keypath-value-is-nil-but-if-let-succeeds/77703)

<div class="topic-metadata">

**Author:** [@bdkjones](https://forums.swift.org/u/bdkjones)\
**Replies:** 3\
**Last updated:** [February 7, 2025, 5:47pm UTC](https://forums.swift.org/t/keypath-value-is-nil-but-if-let-succeeds/77703 "2025-02-07T17:47:00Z")

</div>

I've encountered an unexpected outcome with KeyPath and I'm trying to understand how I'm getting to it. Consider this playground: Expected: The if-let should fail because foo.child is nil. Actual: The if-let succe…

---

## [Transform AnyKeyPath back to ReferenceWritableKeyPath Dynamically](https://forums.swift.org/t/transform-anykeypath-back-to-referencewritablekeypath-dynamically/77192)

<div class="topic-metadata">

**Author:** [@bdkjones](https://forums.swift.org/u/bdkjones)\
**Replies:** 11\
**Last updated:** [February 3, 2025, 4:20pm UTC](https://forums.swift.org/t/transform-anykeypath-back-to-referencewritablekeypath-dynamically/77192 "2025-02-03T16:20:26Z")

</div>

Context I'm writing an abstraction layer that translates Swift model objects into Couchbase database records and back again. I'm struggling to do this in a generic, extendable way with KeyPath. Consider this model objec…

---

## [Any Plans to Enhance KeyPath Ergonomics?](https://forums.swift.org/t/any-plans-to-enhance-keypath-ergonomics/77604)

<div class="topic-metadata">

**Author:** [@bdkjones](https://forums.swift.org/u/bdkjones)\
**Replies:** 1\
**Last updated:** [February 3, 2025, 12:10pm UTC](https://forums.swift.org/t/any-plans-to-enhance-keypath-ergonomics/77604 "2025-02-03T12:10:58Z")

</div>

Consider this playground: The Pain Point Once a KeyPath is type-erased to AnyKeyPath, it becomes really difficult to use it in any sort of dynamic way. (i.e. in a way where you don't have to explicitly type out the s…

---

## [Checking equality of key paths not working as I expected](https://forums.swift.org/t/checking-equality-of-key-paths-not-working-as-i-expected/76594)

<div class="topic-metadata">

**Author:** [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Replies:** 2\
**Last updated:** [December 13, 2024, 2:18pm UTC](https://forums.swift.org/t/checking-equality-of-key-paths-not-working-as-i-expected/76594 "2024-12-13T14:18:07Z")

</div>

I ran into somewhat unexpected behavior when trying to compare key paths for equality. My best guess is that the key path that I'm passing into the doSomething(with:) method references the concrete type's implementation,…

---

## [6.0 Strict Concurrency Warning when KeyPath marked as Sendable](https://forums.swift.org/t/6-0-strict-concurrency-warning-when-keypath-marked-as-sendable/72566)

<div class="topic-metadata">

**Author:** [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Replies:** 2\
**Last updated:** [June 18, 2024, 3:29am UTC](https://forums.swift.org/t/6-0-strict-concurrency-warning-when-keypath-marked-as-sendable/72566 "2024-06-18T03:29:51Z")

</div>

Hi! I'm working with some KeyPath types and Strict Concurrency. I'm seeing some warnings and trying to following along with SE-0418\[1\]… but I'm not sure I understand how I can make the compiler happy. Here is a (simple)…

---

## [Is \\MyType syntax taken, would it be a good synonym for…](https://forums.swift.org/t/is-mytype-syntax-taken-would-it-be-a-good-synonym-for/72494)

<div class="topic-metadata">

**Author:** [@jpmhouston](https://forums.swift.org/u/jpmhouston)\
**Replies:** 7\
**Last updated:** [June 16, 2024, 5:11am UTC](https://forums.swift.org/t/is-mytype-syntax-taken-would-it-be-a-good-synonym-for/72494 "2024-06-16T05:11:15Z")

</div>

I struggle whenever I read or have to write MyType.self. This means you’re referring to a type and not using it … there’s a more accurate description involving instance of meta types, I know, but thinking on those terms …

---

## [\[Pitch\] Metatype Keypaths](https://forums.swift.org/t/pitch-metatype-keypaths/70767)

<div class="topic-metadata">

**Author:** [@amritpan](https://forums.swift.org/u/amritpan)\
**Replies:** 23\
**Last updated:** [April 30, 2024, 8:02pm UTC](https://forums.swift.org/t/pitch-metatype-keypaths/70767 "2024-04-30T20:02:19Z")

</div>

Hello everyone, The following is a pitch for metatype keypaths in the Swift language. We welcome any feedback or questions about this pitch! \[Pitch\] Metatype Keypaths Proposal: SE-NNNN Authors: Amritpan Kaur, Pavel Y…

---

## [Does ‘KeyPath’ produce memory leaks?](https://forums.swift.org/t/does-keypath-produce-memory-leaks/64050)

<div class="topic-metadata">

**Author:** [@DevYeom](https://forums.swift.org/u/DevYeom)\
**Replies:** 4\
**Last updated:** [April 15, 2024, 2:04pm UTC](https://forums.swift.org/t/does-keypath-produce-memory-leaks/64050 "2024-04-15T14:04:46Z")

</div>

Hello there! When using KeyPath, it seems like the object remains in memory. Do you think it's a bug? Or is it because for performance reasons, it is kept in memory like a static variable after the first use? Please l…

---

## [Key paths and unavailable setters](https://forums.swift.org/t/key-paths-and-unavailable-setters/71034)

<div class="topic-metadata">

**Author:** [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Replies:** 15\
**Last updated:** [April 3, 2024, 7:04pm UTC](https://forums.swift.org/t/key-paths-and-unavailable-setters/71034 "2024-04-03T19:04:07Z")

</div>

Currently, a key path formed to a property that has an unavailable setter are WritableKeyPaths. This seems like a bug, since it allows code like dynamic member lookup to quietly compile what should crash at runtime. Toy …

---

## [The Algebra of KeyPaths and @dynamicMemberLookup](https://forums.swift.org/t/the-algebra-of-keypaths-and-dynamicmemberlookup/70087)

<div class="topic-metadata">

**Author:** [@rvsrvs](https://forums.swift.org/u/rvsrvs)\
**Replies:** 9\
**Last updated:** [February 16, 2024, 7:08pm UTC](https://forums.swift.org/t/the-algebra-of-keypaths-and-dynamicmemberlookup/70087 "2024-02-16T19:08:27Z")

</div>

Consider the following: struct A { } struct B { var a: A } struct C { var b: B } struct D { var c: C; var a: A } let kp1: KeyPath\<D, C\> = \\D.c let kp2: KeyPath\<D, A\> = \\D.c.b.a let kp3: KeyPath\<D, A\> = \\D.a print("\\(k…

---

## [SE-0418: Inferring Sendable for methods and key path literals](https://forums.swift.org/t/se-0418-inferring-sendable-for-methods-and-key-path-literals/68999)

<div class="topic-metadata">

**Author:** [@beccadax](https://forums.swift.org/u/beccadax)\
**Replies:** 6\
**Last updated:** [January 3, 2024, 12:17am UTC](https://forums.swift.org/t/se-0418-inferring-sendable-for-methods-and-key-path-literals/68999 "2024-01-03T00:17:28Z")

</div>

Hello Swift community, The review of "Inferring Sendable for methods and key path literals" begins now and runs through January 1, 2024. The proposal is available here: swift-evolution/proposals/0418-inferring-sendabl…

---

## [SE-0416: Subtyping for keypath literals as functions](https://forums.swift.org/t/se-0416-subtyping-for-keypath-literals-as-functions/68984)

<div class="topic-metadata">

**Author:** [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Replies:** 16\
**Last updated:** [January 2, 2024, 11:34pm UTC](https://forums.swift.org/t/se-0416-subtyping-for-keypath-literals-as-functions/68984 "2024-01-02T23:34:09Z")

</div>

Hello, Swift community. The review of SE-0416: Subtyping for keypath literals as functions begins now and runs until January 2nd, 2024. Reviews are an important part of the Swift evolution process. All review feedback …

---

## [Getting KeyPaths to members automatically using Mirror](https://forums.swift.org/t/getting-keypaths-to-members-automatically-using-mirror/21207)

<div class="topic-metadata">

**Author:** [@porterchild](https://forums.swift.org/u/porterchild)\
**Replies:** 16\
**Last updated:** [December 14, 2023, 3:17pm UTC](https://forums.swift.org/t/getting-keypaths-to-members-automatically-using-mirror/21207 "2023-12-14T15:17:31Z")

</div>

I've discovered an ability of Swift but I'm not sure it's safe. After modifying this StackOverflow answer, I've got a protocol that will give you, for any conforming object, a dictionary of \[member variable name : its k…

---

## [String(describing:) returns differently for KeyPaths to computed properties between macOS and Linux](https://forums.swift.org/t/string-describing-returns-differently-for-keypaths-to-computed-properties-between-macos-and-linux/68970)

<div class="topic-metadata">

**Author:** [@CrownedPhoenix](https://forums.swift.org/u/CrownedPhoenix)\
**Replies:** 2\
**Last updated:** [December 13, 2023, 4:43pm UTC](https://forums.swift.org/t/string-describing-returns-differently-for-keypaths-to-computed-properties-between-macos-and-linux/68970 "2023-12-13T16:43:13Z")

</div>

Given the following code: struct Thing { var name: String { "name" } } print(\\Thing.name) print(String(describing: \\Thing.name)) print("\\(\\Thing.name)") The output on macOS, M1 Pro, Ventura 13.5.2: \\Thing.name \\T…

---

## [Swift won't compile dictionary with ReferenceWritableKeyPath unless its a class property](https://forums.swift.org/t/swift-wont-compile-dictionary-with-referencewritablekeypath-unless-its-a-class-property/67148)

<div class="topic-metadata">

**Author:** [@dhoerl](https://forums.swift.org/u/dhoerl)\
**Replies:** 3\
**Last updated:** [September 6, 2023, 7:11pm UTC](https://forums.swift.org/t/swift-wont-compile-dictionary-with-referencewritablekeypath-unless-its-a-class-property/67148 "2023-09-06T19:11:24Z")

</div>

This code compiles just fine: @objcMembers final class DriversLicense1: NSObject { private let map: \[String: ReferenceWritableKeyPath\<DriversLicense1, String\>\] = \[ "DAA": \\DriversLicense1.nameFull, \] …

---

## [Modifications with keyPaths in protocol extension](https://forums.swift.org/t/modifications-with-keypaths-in-protocol-extension/65297)

<div class="topic-metadata">

**Author:** [@Rolfrider](https://forums.swift.org/u/Rolfrider)\
**Replies:** 6\
**Last updated:** [August 21, 2023, 8:20am UTC](https://forums.swift.org/t/modifications-with-keypaths-in-protocol-extension/65297 "2023-08-21T08:20:00Z")

</div>

Basically my idea was to have like generic Builder protocol. I wanted to have pre implemented set method with keyPath. For some reason it doesn't compile with error message: "Cannot assign through subscript: 'self' is im…

---

## ["Observing" changes to arbitrary key paths on move-only structs](https://forums.swift.org/t/observing-changes-to-arbitrary-key-paths-on-move-only-structs/64436)

<div class="topic-metadata">

**Author:** [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Replies:** 14\
**Last updated:** [July 17, 2023, 3:49pm UTC](https://forums.swift.org/t/observing-changes-to-arbitrary-key-paths-on-move-only-structs/64436 "2023-07-17T15:49:53Z")

</div>

I've been following the discussions around SE-0395: Observability as well as the counter-points raised by @gwendal.roue here, and some new ideas related to the topic formed in my mind that I want to share and hear feedba…

---

## [New keypath subscript](https://forums.swift.org/t/new-keypath-subscript/65495)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 2\
**Last updated:** [June 16, 2023, 2:16pm UTC](https://forums.swift.org/t/new-keypath-subscript/65495 "2023-06-16T14:16:22Z")

</div>

If I override: subscript\<T\>(keyPath keyPath: WritableKeyPath\<Self, T\>) -\> T { ... } it won't be called when I'm getting/setting fields directly. Could we have another subscript in addition, say: subscript\<T\>(proxy ke…

---

## [dynamicMemberLookup bug?](https://forums.swift.org/t/dynamicmemberlookup-bug/65593)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 2\
**Last updated:** [June 15, 2023, 1:14pm UTC](https://forums.swift.org/t/dynamicmemberlookup-bug/65593 "2023-06-15T13:14:52Z")

</div>

I have this struct: struct Foo { var name: String = "hello" var count: Int = 42 } When I add @dynamicMemberLookup on it compiler rightfully complains: So, I've added the relevant subscript implementation: @d…

---

## [Type cannot be converted to itself](https://forums.swift.org/t/type-cannot-be-converted-to-itself/65339)

<div class="topic-metadata">

**Author:** [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Replies:** 3\
**Last updated:** [June 3, 2023, 2:28am UTC](https://forums.swift.org/t/type-cannot-be-converted-to-itself/65339 "2023-06-03T02:28:00Z")

</div>

Here's an innocuous-looking extension on Sequence: extension Sequence { func removeNils\<T\>() -\> \[T\] where Element == T? { self.compactMap(\\T?.self) } } However, it doesn't even compile: error: key path valu…

---

## [Read-only keyPath to an Optional](https://forums.swift.org/t/read-only-keypath-to-an-optional/64438)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 3\
**Last updated:** [April 17, 2023, 6:15pm UTC](https://forums.swift.org/t/read-only-keypath-to-an-optional/64438 "2023-04-17T18:15:42Z")

</div>

Found this disparity with a keypath to an optional field: struct Foo { var point: CGPoint? = .zero } var foo = Foo() var a = \\Foo.point!.x var b = \\Foo.point?.x foo.point!.x = 42 // ✅ foo.point?.x = 42 // ✅ f…

---

## [Are KeyPaths Codable?](https://forums.swift.org/t/are-keypaths-codable/23920)

<div class="topic-metadata">

**Author:** [@porterchild](https://forums.swift.org/u/porterchild)\
**Replies:** 10\
**Last updated:** [April 13, 2023, 11:03pm UTC](https://forums.swift.org/t/are-keypaths-codable/23920 "2023-04-13T23:03:13Z")

</div>

I've a pretty simple question: Can a KeyPath be encoded and decoded? Our team decided to use KeyPaths partly because of a weakness of pointers, which is that a pointer is only valid addressing memory on the machine it w…

---

## [Query into dynamic data using static key paths](https://forums.swift.org/t/query-into-dynamic-data-using-static-key-paths/36728)

<div class="topic-metadata">

**Author:** [@lightsprint09](https://forums.swift.org/u/lightsprint09)\
**Replies:** 4\
**Last updated:** [March 10, 2023, 6:55am UTC](https://forums.swift.org/t/query-into-dynamic-data-using-static-key-paths/36728 "2023-03-10T06:55:28Z")

</div>

Hello, I am currently porting Automerge to swift. It is a CRDT. You can create a document, sync the content to different devices, edit concurrently, sync it back and end up with consistent data on every device. (It is a…

[Next page](https://forums.swift.org/tag/keypaths/56.md?match_all_tags=true&page=1&tags%5B%5D=keypaths)
