---
title: "setValue(_:forKey:)"
framework: objectivec
role: symbol
role_heading: Instance Method
path: "objectivec/nsobject-swift.class/setvalue(_:forkey:)"
---

# setValue(_:forKey:)

Sets the property of the receiver specified by a given key to a given value.

## Declaration

```swift
func setValue(_ value: Any?, forKey key: String)
```

## Parameters

- `value`: The value for the property identified by key.
- `key`: The name of one of the receiver’s properties.

## Discussion

Discussion If key identifies a to-one relationship, relate the object specified by value to the receiver, unrelating the previously related object if there was one. Given a collection object and a key that identifies a to-many relationship, relate the objects contained in the collection to the receiver, unrelating previously related objects if there were any. The search pattern that setValue:forKey: uses is described in Accessor Search Patterns in Key-Value Coding Programming Guide. In a reference-counted environment, if the instance variable is accessed directly, value is retained.

## See Also

### Setting Values

- [setValue(_:forKeyPath:)](objectivec/nsobject-swift.class/setvalue(_:forkeypath:).md)
- [setValuesForKeys(_:)](objectivec/nsobject-swift.class/setvaluesforkeys(_:).md)
- [setNilValueForKey(_:)](objectivec/nsobject-swift.class/setnilvalueforkey(_:).md)
- [setValue(_:forUndefinedKey:)](objectivec/nsobject-swift.class/setvalue(_:forundefinedkey:).md)
