Instance Method

set(_:forKey:)

Sets a data object for the specified key in the key-value store.

Declaration

func set(_ aData: Data?, forKey aKey: String)

Parameters

aData

The data object to store. The total size of this data object (including any object overhead) must not exceed 1 MB.

aKey

The key under which to store the value. The length of this key must not exceed 64 bytes using UTF8 encoding.

Discussion

Using an NSData object as a value in key-value storage lets you store arbitrary data. For example, in a game app, you can use it to store game state to iCloud.

Be sure to exercise caution when storing a data object. Because it is available to be read and modified by every instance of your app attached to a user’s iCloud account, some of which may be older versions or running on another platform, you must diligently protect a data object’s integrity. For more information, see “Exercise Caution When Using NSData Objects as Values” in iCloud Design Guide.

See Also

Setting Values

func set([Any]?, forKey: String)

Sets an array object for the specified key in the key-value store.

func set(Bool, forKey: String)

Sets a Boolean value for the specified key in the key-value store.

func set([String : Any]?, forKey: String)

Sets a dictionary object for the specified key in the key-value store.

func set(Double, forKey: String)

Sets a double value for the specified key in the key-value store.

func set(Int64, forKey: String)

Sets a long long value for the specified key in the key-value store.

func set(Any?, forKey: String)

Sets an object for the specified key in the key-value store.

func set(String?, forKey: String)

Sets a string object for the specified key in the key-value store.