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

# setObject(_:forKey:)

Stores an object in the record using the specified key.

## Declaration

```swift
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 doc://com.apple.cloudkit/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.

## See Also

### Accessing a Record’s Fields

- [object(forKey:)](cloudkit/ckrecordkeyvaluesetting/object(forkey:).md)
- [subscript(_:)](cloudkit/ckrecordkeyvaluesetting/subscript(_:).md)
- [allKeys()](cloudkit/ckrecordkeyvaluesetting/allkeys().md)
- [changedKeys()](cloudkit/ckrecordkeyvaluesetting/changedkeys().md)
