<!--
{
  "availability" : [
    "iOS: 3.0.0 -",
    "iPadOS: 3.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.4.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreData",
  "identifier" : "/documentation/CoreData/NSManagedObject/setValue(_:forKey:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core Data"
    ],
    "preciseIdentifier" : "c:objc(cs)NSManagedObject(im)setValue:forKey:"
  },
  "title" : "setValue(_:forKey:)"
}
-->

# setValue(_:forKey:)

Sets the specified property of the managed object to the specified value.

```
func setValue(_ value: Any?, forKey key: String)
```

## Parameters

`value`

The new value for the property specified by `key`.

`key`

The name of one of the receiver’s properties.

## Discussion

If `key` is not a property defined by the model, the method raises an exception. If `key` identifies a to-one relationship, relates the object specified by `value` to the receiver, unrelating the previously related object if there was one. Given a collection object and a key that identifies a to-many relationship, relates the objects contained in the collection to the receiver, unrelating previously related objects if there were any.

This method is overridden by `NSManagedObject` to access the managed object’s generic dictionary storage unless the receiver’s class explicitly provides key-value coding compliant accessor methods for `key`.

> Important:
> You must not override this method.

## See Also

[`setObservationInfo(_:)`](/documentation/CoreData/NSManagedObject/setObservationInfo(_:))

Sets the observation info of the managed object.



---

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)