---
title: "update(with:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/set/update(with:)-2n6tk"
---

# update(with:)

Inserts the given element into the set unconditionally.

## Declaration

```swift
@discardableResult mutating func update(with newMember: Element) -> Element?
```

## Parameters

- `newMember`: An element to insert into the set.

## Return Value

Return Value An element equal to newMember if the set already contained such a member; otherwise, nil. In some cases, the returned element may be distinguishable from newMember by identity comparison or some other means.

## Discussion

Discussion If an element equal to newMember is already contained in the set, newMember replaces the existing element. In this example, an existing element is inserted into classDays, a set of days of the week. enum DayOfTheWeek: Int {     case sunday, monday, tuesday, wednesday, thursday,         friday, saturday }

var classDays: Set<DayOfTheWeek> = [.monday, .wednesday, .friday] print(classDays.update(with: .monday)) // Prints "Optional(DayOfTheWeek.monday)"

## See Also

### Adding Elements

- [insert(_:)](swift/set/insert(_:)-nads.md)
- [insert(_:)](swift/set/insert(_:)-yar4.md)
- [update(with:)](swift/set/update(with:)-7r2g.md)
- [reserveCapacity(_:)](swift/set/reservecapacity(_:).md)
