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

# takeStoredValue(_:forKey:)

Sets the value of the property identified by a given key.

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

## Discussion

This method is used to initialize the receiver with values from an object store (generally, this storage is ultimately from a database) or to restore a value from a snapshot. The default implementation is similar to the implementation of [`takeValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeValue(_:forKey:)), but it resolves `key` with a different method/instance variable search order:

1. Searches for a private accessor method based on `key` (a method preceded by an underbar). For example, with a `key` of “lastName”, [`takeStoredValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeStoredValue(_:forKey:)) looks for a method named `_setLastName:`.
2. If a private accessor is not found, searches for an instance variable based on `key` and sets its `value` directly. For example, with a `key` of “lastName”, [`takeStoredValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeStoredValue(_:forKey:)) looks for an instance variable named `_lastName` or `lastName`.
3. If neither a private accessor nor an instance variable is found, [`takeStoredValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeStoredValue(_:forKey:)) searches for a public accessor method based on `key`. For the `key` “lastName”, this would be `setLastName:`.
4. If `key` is unknown, [`takeStoredValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeStoredValue(_:forKey:)) calls [`handleTakeValue(_:forUnboundKey:)`](/documentation/ObjectiveC/NSObject-swift.class/handleTakeValue(_:forUnboundKey:)).

This different search order allows an object to bypass processing that is performed before setting a value through a public API. However, if you always want to use the search order in [`takeValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeValue(_:forKey:)), you can implement the class method [`useStoredAccessor()`](/documentation/ObjectiveC/NSObject-swift.class/useStoredAccessor()) to return [`NO`](/documentation/ObjectiveC/NO). And as with [`value(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/value(forKey:)), you can prevent direct access of an instance variable with the class method [`accessInstanceVariablesDirectly`](/documentation/ObjectiveC/NSObject-swift.class/accessInstanceVariablesDirectly).

---

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)