In Objective-C there were three basic types of Data Structures, NSArray, NSDictionary, and NSSet. In Objective-C, the immutable and mutable forms were separate, so you also had NSMutableArray, NSMutableDictionary, and NSMutableSet.
In Swift we were greeted with Arrays and Dictionaries, but there was no Set to be found! You had to go back to Objective-C’s NSSet to work with them. You can read about the other two Data Structures back in the previous posts Arrays and their Methods in Swift and Swift Dictionary Quick Reference
These are most of the methods and properties available for the Set type:
|This initializes a completely empty Set.
|Initialize Set from Array Literal
|let abcSet = Set([“A”, “B”, “C”, “D”])
|This creates a Set that contains the Strings of the first 4 letters of the alphabet.
|Check Set Membership
|This returns a Bool of whether foodSet already contains the Swift String “Sandwiches”.
|This will insert the Swift String “Salad” into the Set.
|This will remove a value from the array. This will return an Optional of the Set’s type, with the value if it was there, or nil if it wasn’t.
|Remove All Values
|This will completely empty the Set of all members.
|Number of Elements
|Returns an Int of how many elements are in the Set.
|Check if Empty
|Returns a Bool of true if the foodSet has no members. It returns false if there are any members in the Set.
|Check if Subset
|Returns true if entree set is a subset of foodSet. That is, if all of entreeSet’s members are in foodSet.
|Check if Strict Subset
|Returns true if entree set is a subset of foodSet, but not an exact copy.
|Check if Superset
|Returns true if foodSet is a superset of entreeSet. That is, if foodSet contains all members of entreeSet.
|Check if Strict Superset
|Returns true only if sameFoodSet is a superset of foodSet, but not an exact copy.
|Check if no members same
|Returns true if the two Sequences have no members in common.
|Returns a new Set containing the members of both foodSet and otherFoods.
|Combine Sets In Place
|Mutates the otherFoods Set to add the members of foodSet to it.
|Subtract One Set From Another
|Returns a new Set with the values of entreeSet removed from otherFoods, if they were present.
|Subtract One Set From Another In Place
|Mutates the otherFoods Set to subtract an values that were specified in the entreeSet (like above).
|Create Set of Common Members
|Returns a new Set with the values that were in common between moreFoods and entreeSet.
|Create Set of Common Members In Place
|Mutates the moreFoods Set to perform the intersect method above.
|Create Set of Uncommon Members
|Returns a new Set containing the values that were in either moreFoods or dessertsSet, but NOT both.
|Create Set of Uncommon Members In Place
|Mutates the moreFoods Set with the result of the exclusiveOr method above.