<!--
{
  "availability" : [
    "iOS: 5.0.0 -",
    "iPadOS: 5.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.7.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSUbiquitousKeyValueStore/set(_:forKey:)-3ga7z",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSUbiquitousKeyValueStore(im)setData:forKey:"
  },
  "title" : "set(_:forKey:)"
}
-->

# set(_:forKey:)

Sets the value of the specified key to a data object.

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

## Parameters

`aData`

The data object to save to the iCloud key-value store.

`aKey`

The key to associate with the value.

## Discussion

To store types that aren’t property list objects, archive them to an [`NSData`](/documentation/Foundation/NSData) object
first and add that object to the store using this method. Exercise caution when saving
custom objects to iCloud. Instances of your app on a person’s other devices must also
be able to extract the objects and use them. Design your objects to be portable, and design
new versions of your app to support previous versions of your custom types.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)