Contents

object(forKey:)

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

Declaration

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

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

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