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

# setObject(_:forKey:)

Stores an object in the record using the specified key.

```
func setObject(_ object: (any __CKRecordObjCValue)?, forKey key: String)
```

## Parameters

`object`

The object to store using the specified key. It must be one of the data types in [`Supported Data Types`](/documentation/CloudKit/CKRecord#Supported-Data-Types). You receive an error if you use a data type that CloudKit doesn’t support. If you specify `nil`, CloudKit removes any object that the record associates with the key.

`key`

The key to associate with `object`. Use this key to retrieve the value later. A key must consist of one or more alphanumeric characters and must start with a letter. CloudKit permits the use of underscores, but not spaces. Avoid using a key that matches the name of any property of `CKRecord`.

---

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)