# ReferenceWritableKeyPath to a struct property (like SwiftUI’s @State)

**URL:** <https://forums.swift.org/t/referencewritablekeypath-to-a-struct-property-like-swiftui-s-state/29835>\
**Category:** Using Swift\
**Tags:** swiftui, propertywrapper\
**Created:** [October 15, 2019, 10:44am UTC](https://forums.swift.org/t/referencewritablekeypath-to-a-struct-property-like-swiftui-s-state/29835 "2019-10-15T10:44:41Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![marco.masser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/marco.masser/32/6010_2.png) [@marco.masser](https://forums.swift.org/u/marco.masser)\
**Post date:** [October 15, 2019, 11:56am UTC](https://forums.swift.org/t/referencewritablekeypath-to-a-struct-property-like-swiftui-s-state/29835/3 "2019-10-15T11:56:00Z")

</div>

Ah, thanks for the hint about `nonmutating set`. I can confirm that:

```swift
@propertyWrapper struct MyWrapper {
    var wrappedValue: Int {
        get { 0 }
        nonmutating set { }
    }
}

struct MyStruct {
    @MyWrapper var i: Int
}

print(type(of: \MyStruct.i)) // ReferenceWritableKeyPath<MyStruct, Int>

```

Removing just the `nonmutating` keyword results in a `WritableKeyPath` again.

I’m sure somebody put a whole lot of thought into this and there are good reasons why it is the way it is. Now I just need to figure out these reasons 🤔

* * *

Edit: What I’d come up with if put on the spot is: A struct property that can be set without mutating the struct itself must be something that “delegates” the mutation to some place behind a pointer, like the example by [Rob Mayoff](https://forums.swift.org/u/mayoff) with the `UnsafeMutablePointer` in [this post](https://forums.swift.org/t/why-i-can-mutate-state-var-how-does-state-property-wrapper-work-inside/27209/2). Another option would be a struct that delegates its storage to a singleton like `UserDefaults`.  
The crucial thing is that there must be a pointer involved somewhere, i.e. a _reference_ to something else.

Therefore, key paths to properties of structs that have nonmutating setters are `ReferenceWritableKeyPath`s.

I’m not sure this is convincing, but that’s what I’d guess.

---

_[View the full topic](https://forums.swift.org/t/referencewritablekeypath-to-a-struct-property-like-swiftui-s-state/29835)._
