CustomValueOptionSet
public protocol CustomValueOptionSet : OptionSet where Self == Self.Element, Self.RawValue : FixedWidthInteger
Option set implementation which allows each option to have custom string value attached.
-
Provides a text value description for user-provided options.
The option set will recognize a custom option if it’s unique
rawValue
flag is set andcustomOptionsByRawValue
contains a description for that flag. Use theupdate(customOption:comparisonPolicy:)
method to append a custom option.Declaration
Swift
var customOptionsByRawValue: [RawValue : CustomValue] { get set }
-
Returns a Boolean value that indicates whether the given element exists in the set.
This example uses the
contains(_:)
method to test whether an integer is a member of a set of prime numbers.let primes: Set = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] let x = 5 if primes.contains(x) { print("\(x) is prime!") } else { print("\(x). Not prime.") } // Prints "5 is prime!"
Declaration
Swift
func contains(_ member: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Bool
Parameters
member
An element to look for in the set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
true
ifmember
exists in the set; otherwise,false
. -
Returns a new set with the elements of both this and the given set.
In the following example, the
attendeesAndVisitors
set is made up of the elements of theattendees
andvisitors
sets:let attendees: Set = ["Alicia", "Bethany", "Diana"] let visitors = ["Marcia", "Nathaniel"] let attendeesAndVisitors = attendees.union(visitors) print(attendeesAndVisitors) // Prints "["Diana", "Nathaniel", "Bethany", "Alicia", "Marcia"]"
If the set already contains one or more elements that are also in
other
, the existing members are kept.let initialIndices = Set(0..<5) let expandedIndices = initialIndices.union([2, 3, 6, 7]) print(expandedIndices) // Prints "[2, 4, 6, 7, 0, 1, 3]"
Note
if this set and
other
contain elements that are equal but distinguishable (e.g. via===
), which of these elements is present in the result is unspecified.Declaration
Swift
func union(_ other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Self.Element
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
A new set with the unique elements of this set and
other
. -
Adds the elements of the given set to the set.
In the following example, the elements of the
visitors
set are added to theattendees
set:var attendees: Set = ["Alicia", "Bethany", "Diana"] let visitors: Set = ["Diana", "Marcia", "Nathaniel"] attendees.formUnion(visitors) print(attendees) // Prints "["Diana", "Nathaniel", "Bethany", "Alicia", "Marcia"]"
If the set already contains one or more elements that are also in
other
, the existing members are kept.var initialIndices = Set(0..<5) initialIndices.formUnion([2, 3, 6, 7]) print(initialIndices) // Prints "[2, 4, 6, 7, 0, 1, 3]"
Declaration
Swift
mutating func formUnion(_ other: Self, comparisonPolicy: CustomOptionComparisonPolicy)
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
. -
Returns a new set with the elements that are common to both this set and the given set.
In the following example, the
bothNeighborsAndEmployees
set is made up of the elements that are in both theemployees
andneighbors
sets. Elements that are in only one or the other are left out of the result of the intersection.let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"] let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"] let bothNeighborsAndEmployees = employees.intersection(neighbors) print(bothNeighborsAndEmployees) // Prints "["Bethany", "Eric"]"
Note
if this set and
other
contain elements that are equal but distinguishable (e.g. via===
), which of these elements is present in the result is unspecified.Declaration
Swift
func intersection(_ other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Self.Element
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
A new set.
-
Removes the elements of this set that aren’t also in the given set.
In the following example, the elements of the
employees
set that are not also members of theneighbors
set are removed. In particular, the names"Alicia"
,"Chris"
, and"Diana"
are removed.var employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"] let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"] employees.formIntersection(neighbors) print(employees) // Prints "["Bethany", "Eric"]"
Declaration
Swift
mutating func formIntersection(_ other: Self, comparisonPolicy: CustomOptionComparisonPolicy)
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
. -
Returns a new set with the elements that are either in this set or in the given set, but not in both.
In the following example, the
eitherNeighborsOrEmployees
set is made up of the elements of theemployees
andneighbors
sets that are not in bothemployees
andneighbors
. In particular, the names"Bethany"
and"Eric"
do not appear ineitherNeighborsOrEmployees
.let employees: Set = ["Alicia", "Bethany", "Diana", "Eric"] let neighbors: Set = ["Bethany", "Eric", "Forlani"] let eitherNeighborsOrEmployees = employees.symmetricDifference(neighbors) print(eitherNeighborsOrEmployees) // Prints "["Diana", "Forlani", "Alicia"]"
Declaration
Swift
func symmetricDifference(_ other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Self.Element
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
A new set.
-
Removes the elements of the set that are also in the given set and adds the members of the given set that are not already in the set.
In the following example, the elements of the
employees
set that are also members ofneighbors
are removed fromemployees
, while the elements ofneighbors
that are not members ofemployees
are added toemployees
. In particular, the names"Bethany"
and"Eric"
are removed fromemployees
while the name"Forlani"
is added.var employees: Set = ["Alicia", "Bethany", "Diana", "Eric"] let neighbors: Set = ["Bethany", "Eric", "Forlani"] employees.formSymmetricDifference(neighbors) print(employees) // Prints "["Diana", "Forlani", "Alicia"]"
Declaration
Swift
mutating func formSymmetricDifference(_ other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy)
Parameters
other
A set of the same type.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
. -
Returns a new set containing the elements of this set that do not occur in the given set.
In the following example, the
nonNeighbors
set is made up of the elements of theemployees
set that are not elements ofneighbors
:let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"] let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"] let nonNeighbors = employees.subtracting(neighbors) print(nonNeighbors) // Prints "["Diana", "Chris", "Alicia"]"
Declaration
Swift
func subtracting(_ other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Self.Element
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
A new set.
-
Removes the elements of the given set from this set.
In the following example, the elements of the
employees
set that are also members of theneighbors
set are removed. In particular, the names"Bethany"
and"Eric"
are removed fromemployees
.var employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"] let neighbors: Set = ["Bethany", "Eric", "Forlani", "Greta"] employees.subtract(neighbors) print(employees) // Prints "["Diana", "Chris", "Alicia"]"
Declaration
Swift
mutating func subtract(_ other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy)
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
. -
Inserts the given element in the set if it is not already present.
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 intoclassDays
, a set of days of the week. When an existing element is inserted, theclassDays
set does not change.enum DayOfTheWeek: Int { case sunday, monday, tuesday, wednesday, thursday, friday, saturday } var classDays: Set<DayOfTheWeek> = [.wednesday, .friday] print(classDays.insert(.monday)) // Prints "(true, .monday)" print(classDays) // Prints "[.friday, .wednesday, .monday]" print(classDays.insert(.friday)) // Prints "(false, .friday)" print(classDays) // Prints "[.friday, .wednesday, .monday]"
Declaration
Swift
mutating func insert(_ newMember: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> (inserted: Bool, memberAfterInsert: Self.Element)
Parameters
newMember
An element to insert into the set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
(true, newMember)
ifnewMember
was not contained in the set. If an element equal tonewMember
was already contained in the set, the method returns(false, oldMember)
, whereoldMember
is the element that was equal tonewMember
. In some cases,oldMember
may be distinguishable fromnewMember
by identity comparison or some other means. -
Removes the given element and any elements subsumed by the given element.
For sets where the set type and element type are the same, like
OptionSet
types, this method returns any intersection between the set and[member]
, ornil
if the intersection is empty.Declaration
Swift
mutating func remove(_ member: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Self.Element?
Parameters
member
The element of the set to remove.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
For ordinary sets, an element equal to
member
ifmember
is contained in the set; otherwise,nil
. In some cases, a returned element may be distinguishable frommember
by identity comparison or some other means. -
Inserts the given element into the set unconditionally.
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 intoclassDays
, 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(.monday)"
For sets where the set type and element type are the same, like
OptionSet
types, this method returns any intersection between the set and[newMember]
, ornil
if the intersection is empty.Declaration
Swift
mutating func update(with newMember: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Self.Element?
Parameters
newMember
An element to insert into the set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
For ordinary sets, an element equal to
newMember
if the set already contained such a member; otherwise,nil
. In some cases, the returned element may be distinguishable fromnewMember
by identity comparison or some other means. -
Inserts the given element into the set unconditionally.
If an element equal to
customOption
is already contained in the set,customOption
replaces the existing element. Otherwise - updates the set contents and fillscustomOptionsByRawValue
accordingly.For sets where the set type and element type are the same, like
OptionSet
types, this method returns any intersection between the set and[customOption]
, ornil
if the intersection is empty.Declaration
Swift
mutating func update(customOption: (RawValue, CustomValue), comparisonPolicy: CustomOptionComparisonPolicy) -> Self.Element?
Parameters
customOption
An element to insert into the set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
For ordinary sets, an element equal to
customOption
if the set already contained such a member; otherwise,nil
. In some cases, the returned element may be distinguishable fromcustomOption
by identity comparison or some other means. -
Returns a Boolean value that indicates whether the set is a subset of another set.
Set A is a subset of another set B if every member of A is also a member of B.
let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"] let attendees: Set = ["Alicia", "Bethany", "Diana"] print(attendees.isSubset(of: employees)) // Prints "true"
Declaration
Swift
func isSubset(of other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Bool
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
true
if the set is a subset ofother
; otherwise,false
. -
Returns a Boolean value that indicates whether the set is a superset of the given set.
Set A is a superset of another set B if every member of B is also a member of A.
let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"] let attendees: Set = ["Alicia", "Bethany", "Diana"] print(employees.isSuperset(of: attendees)) // Prints "true"
Declaration
Swift
func isSuperset(of other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Bool
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
true
if the set is a superset ofpossibleSubset
; otherwise,false
. -
Returns a Boolean value that indicates whether this set is a strict subset of the given set.
Set A is a strict subset of another set B if every member of A is also a member of B and B contains at least one element that is not a member of A.
let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"] let attendees: Set = ["Alicia", "Bethany", "Diana"] print(attendees.isStrictSubset(of: employees)) // Prints "true" // A set is never a strict subset of itself: print(attendees.isStrictSubset(of: attendees)) // Prints "false"
Declaration
Swift
func isStrictSubset(of other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Bool
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
true
if the set is a strict subset ofother
; otherwise,false
. -
Returns a Boolean value that indicates whether this set is a strict superset of the given set.
Set A is a strict superset of another set B if every member of B is also a member of A and A contains at least one element that is not a member of B.
let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"] let attendees: Set = ["Alicia", "Bethany", "Diana"] print(employees.isStrictSuperset(of: attendees)) // Prints "true" // A set is never a strict superset of itself: print(employees.isStrictSuperset(of: employees)) // Prints "false"
Declaration
Swift
func isStrictSuperset(of other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Bool
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
true
if the set is a strict superset ofother
; otherwise,false
. -
Returns a Boolean value that indicates whether the set has no members in common with the given set.
In the following example, the
employees
set is disjoint with thevisitors
set because no name appears in both sets.let employees: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"] let visitors: Set = ["Marcia", "Nathaniel", "Olivia"] print(employees.isDisjoint(with: visitors)) // Prints "true"
Declaration
Swift
func isDisjoint(with other: Self.Element, comparisonPolicy: CustomOptionComparisonPolicy) -> Bool
Parameters
other
A set of the same type as the current set.
comparisonPolicy
comparison method to be used for
customOptionsByRawValue
.Return Value
true
if the set has no elements in common withother
; otherwise,false
.
-
contains(_:)
Extension methodDeclaration
Swift
@discardableResult @inlinable func contains(_ member: Self.Element) -> Bool
-
union(_:)
Extension methodDeclaration
Swift
@discardableResult @inlinable func union(_ other: Self) -> Self
-
intersection(_:)
Extension methodDeclaration
Swift
@discardableResult @inlinable func intersection(_ other: Self) -> Self
-
symmetricDifference(_:)
Extension methodDeclaration
Swift
@discardableResult @inlinable func symmetricDifference(_ other: Self) -> Self
-
insert(_:)
Extension methodDeclaration
Swift
@discardableResult @inlinable mutating func insert(_ newMember: Self.Element) -> (inserted: Bool, memberAfterInsert: Self.Element)
-
remove(_:)
Extension methodDeclaration
Swift
@discardableResult @inlinable mutating func remove(_ member: Self.Element) -> Self.Element?
-
update(with:)
Extension methodDeclaration
Swift
@discardableResult @inlinable mutating func update(with newMember: Self.Element) -> Self.Element?
-
formUnion(_:)
Extension methodDeclaration
Swift
@inlinable mutating func formUnion(_ other: Self)
-
formIntersection(_:)
Extension methodDeclaration
Swift
@inlinable mutating func formIntersection(_ other: Self)
-
formSymmetricDifference(_:)
Extension methodDeclaration
Swift
@inlinable mutating func formSymmetricDifference(_ other: Self)
-
subtracting(_:)
Extension methodDeclaration
Swift
@discardableResult @inlinable func subtracting(_ other: Self) -> Self
-
isSubset(of:)
Extension methodDeclaration
Swift
@discardableResult @inlinable func isSubset(of other: Self) -> Bool
-
isDisjoint(with:)
Extension methodDeclaration
Swift
@discardableResult @inlinable func isDisjoint(with other: Self) -> Bool
-
isSuperset(of:)
Extension methodDeclaration
Swift
@discardableResult @inlinable func isSuperset(of other: Self) -> Bool
-
subtract(_:)
Extension methodDeclaration
Swift
@inlinable mutating func subtract(_ other: Self)