<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.6.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCache/setObject(_:forKey:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSCache(im)setObject:forKey:"
  },
  "title" : "setObject(_:forKey:)"
}
-->

# setObject(_:forKey:)

Sets the value of the specified key in the cache.

```
func setObject(_ obj: ObjectType, forKey key: KeyType)
```

## Parameters

`obj`

The object to be stored in the cache.

`key`

The key with which to associate the value.

## Discussion

Unlike an `NSMutableDictionary` object, a cache does not copy the key objects that are put into it.

---

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)