Instance Method


Inserts the given element in the set if it is not already present.

Required. Default implementation provided.


@discardableResult mutating func insert(_ newMember: Self.Element) -> (inserted: Bool, memberAfterInsert: Self.Element)



An element to insert into the set.

Return Value

(true, newMember) if newMember was not contained in the set. If an element equal to newMember was already contained in the set, the method returns (false, oldMember), where oldMember is the element that was equal to newMember. In some cases, oldMember may be distinguishable from newMember by identity comparison or some other means.


If an element equal to newMember is already contained in the set, this method has no effect. In this example, a new element is inserted into classDays, a set of days of the week. When an existing element is inserted, the classDays set does not change.

enum DayOfTheWeek: Int {
    case sunday, monday, tuesday, wednesday, thursday,
        friday, saturday

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

// Prints "(false, .friday)"
// Prints "[.friday, .wednesday, .monday]"

Default Implementations

OptionSet Implementations

func insert(Self.Element) -> (inserted: Bool, memberAfterInsert: Self.Element)

Adds the given element to the option set if it is not already a member.

See Also

Adding and Removing Elements

func update(with: Self.Element) -> Self.Element?

Inserts the given element into the set unconditionally.

Required. Default implementation provided.

func remove(Self.Element) -> Self.Element?

Removes the given element and any elements subsumed by the given element.

Required. Default implementation provided.