Swift Set

«  Swift For In Loops
Swift Sort  »

Set store distinct values of the same type in a collection with no defined ordering. Use Set instead of an array when the order of items is not important, or when need to ensure an item only appears once.

Swift’s Set type is bridged to Foundation’s NSSet class.

For bridging between Set and NSSet, check it at here.

Hash Values

For store in Set, type must be hashable. <- the type must provide a way to compute a hash value for itself.

All of Swift’s basic types (such as String, Int, Double, and Bool) are hashable by default, and can be used as set value types or dictionary key types. Enumeration case values without associated values (as described in Enumerations) are also hashable by default.

Note

You can use your own custom types as set value types or dictionary key types by making them conform to the Hashable protocol from Swift’s standard library. Types that conform to the Hashable protocol must provide a gettable Int property called hashValue. The value returned by a type’s hashValue property is not required to be the same across different executions of the same program, or in different programs.

Because the Hashable protocol conforms to Equatable, conforming types must also provide an implementation of the equals operator (==). The Equatable protocol requires any conforming implementation of == to be an equivalence relation. That is, an implementation of == must satisfy the following three conditions, for all values a, b, and c:

a == a (Reflexivity) a == b implies b == a (Symmetry) a == b && b == c implies a == c (Transitivity)

Create Set

/// use initializer syntax
var letters = Set<Character>()
print("letters is of type Set<Character> with \(letters.count) items.")
// Prints "letters is of type Set<Character> with 0 items."

/// use empty array literal
letters.insert("a")
// letters now contains 1 value of type Character
letters = []
// letters is now an empty set, but is still of type Set<Character>

/// with array literal
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
// favoriteGenres has been initialized with three initial items
// OR
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]

Access and Modify Set

/// numbers of items
print("I have \(favoriteGenres.count) favorite music genres.")
// Prints "I have 3 favorite music genres."

/// add & remove new element
favoriteGenres.insert("Jazz")
favoriteGenres.remove("Rock")

/// check if contains
favoriteGenres.contains("Funk")

Set Operations

  • Use the intersection(_:) method to create a new set with only the values common to both sets.

  • Use the symmetricDifference(_:) method to create a new set with values in either set, but not both.

  • Use the union(_:) method to create a new set with all of the values in both sets.

  • Use the subtracting(_:) method to create a new set with values not in the specified set.

  • Use the “is equal” operator (==) to determine whether two sets contain all of the same values.

  • Use the isSubset(of:) method to determine whether all of the values of a set are contained in the specified set.

  • Use the isSuperset(of:) method to determine whether a set contains all of the values in a specified set.

  • Use the isStrictSubset(of:) or isStrictSuperset(of:) methods to determine whether a set is a subset or superset, but not equal to, a specified set.

  • Use the isDisjoint(with:) method to determine whether two sets have no values in common.

References

https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html

Published on 08 Feb 2020 Find me on Facebook, Twitter!

«  Swift For In Loops
Swift Sort  »

Comments

    Join the discussion for this article at here . Our comments is using Github Issues. All of posted comments will display at this page instantly.