Instance Method

setObject:forKey:

Sets the value for the specified key.

Declaration

- (void)setObject:(id<CKRecordValue>)object forKey:(CKRecordFieldKey)key;

Parameters

obj

The value to assign to the specified key. The object must be one of the types listed in Supported Data Types. It is a programmer error to specify an object of any other type. Specify nil for this parameter to remove the value for the field.

key

The key to associate with obj. Use this key to retrieve the value later. Key names consist of one or more alphanumeric characters and start with a letter. You may also include underscore characters if you do not use an underscore as the first character in the name. Spaces are not allowed in key names. The names of your keys must not match the names of any properties of this class.

Discussion

If the specified key already exists in the record, its old value is deleted and replaced with the one in the obj parameter. This change affects only the local copy of the record. You must save the record to the server again before the change becomes available to other clients.

If the type of the obj parameter differs from the type of the object currently saved on the server, you will encounter an error when you attempt to save this record to the server. For example, if the current value is an NSString object, you will get an error if you change the value to an NSNumber object and save the record.

You access the fields of a CKRecord object the same way you access key-value pairs in an NSMutableDictionary. The CKRecord class defines the objectForKey: 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 field named firstName and get a field named lastName from a given record.

// Equivalent ways to set a value.
[myRecord setObject:[NSDate date] forKey:@"hiredAt"];
myRecord[@"hiredAt"] = [NSDate date];

See Also

Accessing the Record’s Fields

- objectForKey:

Returns the value for the given key stored in the record.

- objectForKeyedSubscript:

Returns the value for the given key stored in the record.

- setObject:forKeyedSubscript:

Adds the specified key-value pair to the record.

- allKeys

Returns an array of strings corresponding to all keys currently in the record.

- changedKeys

Returns an array of strings representing the keys that have changed recently.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software