<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "ObjectiveC",
  "identifier" : "/documentation/ObjectiveC/NSObject-swift.class/setValue(_:forKeyPath:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Objective-C Runtime",
      "ObjectiveC"
    ],
    "preciseIdentifier" : "c:objc(cs)NSObject(im)setValue:forKeyPath:"
  },
  "title" : "setValue(_:forKeyPath:)"
}
-->

# setValue(_:forKeyPath:)

Sets the value for the property identified by a given key path to a given value.

```
func setValue(_ value: Any?, forKeyPath keyPath: String)
```

## Parameters

`value`

The value for the property identified by `keyPath`.

`keyPath`

A key path of the form relationship.property (with one or more relationships): for example “department.name” or “department.manager.lastName.”

## Discussion

The default implementation of this method gets the destination object for each relationship using [`value(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/value(forKey:)), and sends the final object a [`setValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/setValue(_:forKey:)) message.

### Special Considerations

When using this method, and the destination object does not implement an accessor for the value, the default behavior is for that object to retain `value` rather than copy or assign `value`.

## See Also

[`value(forKeyPath:)`](/documentation/ObjectiveC/NSObject-swift.class/value(forKeyPath:))

Returns the value for the derived property identified by a given key path.



---

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)