Swift Sets and their Methods in IOS !

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:

 

Title Example Description
Initialize Set Set<String>() 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 foodSet.contains(“Sandwiches”) This returns a Bool of whether foodSet already contains the Swift String “Sandwiches”.
Insert Value foodSet.insert(“Salad”) This will insert the Swift String “Salad” into the Set.
Remove Value foodSet.remove(“Chips”) 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 foodSet.removeAll() This will completely empty the Set of all members.
Number of Elements foodSet.count Returns an Int of how many elements are in the Set.
Check if Empty foodSet.isEmpty 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 entreeSet.isSubsetOf(foodSet) 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 entreeSet.isStrictSubsetOf(foodSet) Returns true if  entree set is a subset of foodSet, but not an exact copy.
Check if Superset foodSet.isSupersetOf(entreeSet) Returns true if foodSet is a superset of entreeSet.  That is, if foodSet contains all members of entreeSet.
Check if Strict Superset sameFoodSet.isStrictSupersetOf(foodSet) Returns true only if sameFoodSet is a superset of foodSet, but not an exact copy.
Check if no members same foodSet.isDisjointWith(otherFoods) Returns true if the two Sequences have no members in common.
Combine Sets foodSet.union(otherFoods) Returns a new Set containing the members of both foodSet and otherFoods.
Combine Sets In Place otherFoods.unionInPlace(foodSet) Mutates the otherFoods Set to add the members of foodSet to it.
Subtract One Set From Another otherFoods.subtract(entreeSet) Returns a new Set with the values of entreeSet removed from otherFoods, if they were present.
Subtract One Set From Another In Place otherFoods.subtractInPlace(entreeSet) Mutates the otherFoods Set to subtract an values that were specified in the entreeSet (like above).
Create Set of Common Members moreFoods.intersect(entreeSet) Returns a new Set with the values that were in common between moreFoods and entreeSet.
Create Set of Common Members In Place moreFoods.intersectInPlace(entreeSet) Mutates the moreFoods Set to perform the intersect method above.
Create Set of Uncommon Members moreFoods.exclusiveOr(dessertsSet) Returns a new Set containing the values that were in either moreFoods or dessertsSet, but NOT both.
Create Set of Uncommon Members In Place moreFoods.exclusiveOrInPlace(dessertsSet) Mutates the moreFoods Set with the result of the exclusiveOr method above.

Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top