Set Collection in swift

 Sets In Swift

                A set stores distinct values of the same type in a collection with no defined ordering. You can use a set instead of an array when the order of items isn’t important, or when you need to ensure that an item only appears once.



NOTE
Swift’s Set type is bridged to Foundation’s NSSet class.
For more information about using Set with Foundation and Cocoa, see Bridging Between Set and NSSet.

Hash Values for Set Types :

                A type must be hashable in order to be stored in a set—that is, the type must provide a way to compute a hash value for itself. A hash value is an Int value that’s the same for all objects that compare equally, such that if a == b, the hash value of a is equal to the hash value of b.

                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 the Swift standard library. For information about implementing the required hash(into:) method, see Hashable. For information about conforming to protocols, see Protocols.

Set Type Syntax :

               The type of a Swift set is written as Set<Element>, where Element is the type that the set is allowed to store. Unlike arrays, sets don’t have an equivalent shorthand form.

Creating and Initializing an Empty Set :

You can create an empty set of a certain type using initializer syntax:




NOTE
The type of the letters variable is inferred to be Set<Character>, from the type of the initializer.

                Alternatively, if the context already provides type information, such as a function argument or an already typed variable or constant, you can create an empty set with an empty array literal:





Creating a Set with an Array Literal :

                You can also initialize a set with an array literal, as a shorthand way to write one or more values as a set collection.

The example below creates a set called favoriteGenres to store String values:



                The favoriteGenres variable is declared as “a set of String values”, written as Set<String>. Because this particular set has specified a value type of String, it’s only allowed to store String values. Here, the favoriteGenres set is initialized with three String values ("Rock", "Classical", and "Hip hop"), written within an array literal.

NOTE
The favoriteGenres set is declared as a variable (with the var introducer) and not a constant (with the let introducer) because items are added and removed in the examples below.

                A set type can’t be inferred from an array literal alone, so the type Set must be explicitly declared. However, because of Swift’s type inference, you don’t have to write the type of the set’s elements if you’re initializing it with an array literal that contains values of just one type. The initialization of favoriteGenres could have been written in a shorter form instead:



Because all values in the array literal are of the same type, Swift can infer that Set<String> is the correct type to use for the favoriteGenres variable.

Accessing and Modifying a Set :

                You access and modify a set through its methods and properties.

To find out the number of items in a set, check its read-only count property:




Use the Boolean isEmpty property as a shortcut for checking whether the count property is equal to 0:







You can add a new item into a set by calling the set’s insert(_:) method:




                You can remove an item from a set by calling the set’s remove(_:) method, which removes the item if it’s a member of the set, and returns the removed value, or returns nil if the set didn’t contain it. Alternatively, all items in a set can be removed with its removeAll() method.







To check whether a set contains a particular item, use the contains(_:) method.








Iterating Over a Set :

You can iterate over the values in a set with a for-in loop.







For more about the for-in loop, see For-In Loops.

                Swift’s Set type doesn’t have a defined ordering. To iterate over the values of a set in a specific order, use the sorted() method, which returns the set’s elements as an array sorted using the < operator.







Performing Set Operations :

                You can efficiently perform fundamental set operations, such as combining two sets together, determining which values two sets have in common, or determining whether two sets contain all, some, or none of the same values.

Fundamental Set Operations :

                The illustration below depicts two sets—a and b—with the results of various set operations represented by the shaded regions.


  • 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.





Set Membership and Equality :

                    The illustration below depicts three sets—a, b and c—with overlapping regions representing elements shared among sets. Set a is a superset of set b, because a contains all elements in b. Conversely, set b is a subset of set a, because all elements in b are also contained by a. Set b and set c are disjoint with one another, because they share no elements in common.



  • 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.








Want to learn more?

Comments

Popular posts from this blog

POWER BI

Face Recognition

BOOTSTRAP