---
title: "object(forKey:)"
framework: cloudkit
role: symbol
role_heading: Instance Method
path: "cloudkit/ckrecord/object(forkey:)"
---

# object(forKey:)

Returns the object that the record stores for the specified key.

## Declaration

```swift
func object(forKey key: CKRecord.FieldKey) -> (any __CKRecordObjCValue)?
```

## Parameters

- `key`: The string that identifies a field in the record. 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.

## Return Value

Return Value The object for the specified key, or nil if no such key exists in the record.

## Discussion

Discussion New records don’t contain any keys or values. Values are always one of the data types in Supported Data Types. You access the fields of a CKRecord object the same way you access key-value pairs in a dictionary. The CKRecord class defines the object(forKey:) and setObject(_:forKey:) methods for getting and setting values. It also supports dictionary index notation. The following example shows how to use both techniques to set a firstName field and retrieve a lastName field from a record: // Equivalent ways to get a value. var hiredAt = record.object(forKey: "hiredAt") hiredAt = record["hiredAt"]

## See Also

### Accessing the Record’s Fields

- [subscript(_:)](cloudkit/ckrecord/subscript(_:)-51whk.md)
- [subscript(_:)](cloudkit/ckrecord/subscript(_:)-4g91i.md)
- [setObject(_:forKey:)](cloudkit/ckrecord/setobject(_:forkey:).md)
- [allKeys()](cloudkit/ckrecord/allkeys().md)
- [changedKeys()](cloudkit/ckrecord/changedkeys().md)
- [makeIterator()](cloudkit/ckrecord/makeiterator().md)
- [CKRecordKeyValueIterator](cloudkit/ckrecordkeyvalueiterator.md)
- [CKRecordValueProtocol](cloudkit/ckrecordvalueprotocol.md)
- [CKRecordKeyValueSetting](cloudkit/ckrecordkeyvaluesetting.md)
- [CKRecordValue](cloudkit/ckrecordvalue-swift.typealias.md)
