<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Observation/ObservationRegistrar/withMutation(of:keyPath:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Observation"
    ],
    "preciseIdentifier" : "s:11Observation0A9RegistrarV12withMutation2of7keyPath_q0_x_s03KeyG0Cyxq_Gq0_yKXEtKAA10ObservableRzr1_lF"
  },
  "title" : "withMutation(of:keyPath:_:)"
}
-->

# withMutation(of:keyPath:_:)

Identifies mutations to the transactions registered for observers.

```
func withMutation<Subject, Member, T>(of subject: Subject, keyPath: KeyPath<Subject, Member>, _ mutation: () throws -> T) rethrows -> T where Subject : Observable
```

## Parameters

`subject`

An instance of an observable type.

`keyPath`

The key path of an observed property.

## Discussion

This method calls [`willSet(_:keyPath:)`](/documentation/Observation/ObservationRegistrar/willSet(_:keyPath:)) before the mutation. Then it
calls [`didSet(_:keyPath:)`](/documentation/Observation/ObservationRegistrar/didSet(_:keyPath:)) after the mutation.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)