Instance Method

filter(_:)

Returns a new set containing the elements of the set that satisfy the given predicate.

Declaration

func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> Set<Element>

Parameters

isIncluded

A closure that takes an element as its argument and returns a Boolean value indicating whether the element should be included in the returned set.

Return Value

A set of the elements that isIncluded allows.

Discussion

In this example, filter(_:) is used to include only names shorter than five characters.

let cast: Set = ["Vivien", "Marlon", "Kim", "Karl"]
let shortNames = cast.filter { $0.count < 5 }

shortNames.isSubset(of: cast)
// true
shortNames.contains("Vivien")
// false

See Also

Removing Elements

func remove(Element) -> Element?

Removes the specified element from the set.

func removeFirst() -> Element

Removes the first element of the set.

func remove(at: Set<Element>.Index) -> Element

Removes the element at the given index of the set.

func removeAll(keepingCapacity: Bool)

Removes all members from the set.