---
title: "setObject(_:forKey:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nscache/setobject(_:forkey:)"
---

# setObject(_:forKey:)

Sets the value of the specified key in the cache.

## Declaration

```swift
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

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

## See Also

### Adding and Removing Cached Values

- [setObject(_:forKey:cost:)](foundation/nscache/setobject(_:forkey:cost:).md)
- [removeObject(forKey:)](foundation/nscache/removeobject(forkey:).md)
- [removeAllObjects()](foundation/nscache/removeallobjects().md)
