<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Set/update(with:)-2n6tk",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sh6update4withxSgxn_tF"
  },
  "title" : "update(with:)"
}
-->

# update(with:)

Inserts the given element into the set unconditionally.

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

## Parameters

`newMember`

An element to insert into the set.

## 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

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)"
```

---

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)