---
title: "set(_:forKey:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/userdefaults/set(_:forkey:)-8ab6d"
---

# set(_:forKey:)

Sets the value of the specified key to a property list object.

## Declaration

```swift
func set(_ value: Any?, forKey defaultName: String)
```

## Parameters

- `value`: The property-list type to store in the defaults database. If you specify an array or dictionary type, those collections must similarly contain only property list types.
- `defaultName`: The key that contains the setting’s name.

## Discussion

Discussion Use this method to write property list object types to the defaults store. To store types that aren’t property list objects, archive them to a Data object and use this method to save that data object to the defaults store. After you call this method, the system generates a didChangeNotification for registered observers.

## See Also

### Setting the value for a key

- [set(_:forKey:)](foundation/userdefaults/set(_:forkey:)-3nn5m.md)
- [set(_:forKey:)](foundation/userdefaults/set(_:forkey:)-3v852.md)
- [set(_:forKey:)](foundation/userdefaults/set(_:forkey:)-1t5ec.md)
- [set(_:forKey:)](foundation/userdefaults/set(_:forkey:)-2w22f.md)
- [set(_:forKey:)](foundation/userdefaults/set(_:forkey:)-2bqjt.md)
