• Home
  • MAD
  • Concept Series
    • Software Design
    • Software Arch
    • GIT & Github
    • System Design
    • Cloud
    • Database Integration
    • Push Notification
    • API Integration
    • Cocoa PODS
  • DSA
  • Interview
  • Tips&Tricks
  • YT
  • Home
  • MAD
  • Concept Series
    • Software Design
    • Software Arch
    • GIT & Github
    • System Design
    • Cloud
    • Database Integration
    • Push Notification
    • API Integration
    • Cocoa PODS
  • DSA
  • Interview
  • Tips&Tricks
  • YT
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk

MyCodeTips mycodetips-newlogocopy1

  • Home
  • MAD
  • Concept Series
    • Software Design
    • Software Arch
    • GIT & Github
    • System Design
    • Cloud
    • Database Integration
    • Push Notification
    • API Integration
    • Cocoa PODS
  • DSA
  • Interview
  • Tips&Tricks
  • YT
Programming, IOS, Swift

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.
Liked it? Take a second to support Ranjan on Patreon!
become a patron button
  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 1125 Views
AUTHOR
Ranjan

Namaste, My name is Ranjan, I am a graduate from NIT Rourkela. This website is basically about of what i learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures, its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

Next Post
Previous Post

Support us

mycodetips
mycodetips

Follow us @ LinkedIn 2850+

Subscribe for updates

Join 8,213 other subscribers

Latest Posts

  • YT-Featured-solidprinciples
    SOLID Principles of Software Design
  • IOS 16 Features
    Latest features in IOS 16
  • r-language
    How can R language be used for data analysis?
  • wordpress-coding-blog
    Guide To WordPress Coding Standards
  • YT-Featured-Algorithm
    What is Algorithm?
  • Frameworks of IOS
    Frameworks of IOS – Part ( I )
  • NSFileManager or NSPathUtilities
    NSFileManager or NSPathUtilities in Objective-C
  • Passing data between view controllers in Objective-C
    Passing data between view controllers in Objective-C
  • structures-classes-enum
    Structures and Classes in swift !
  • control-system-swift
    Control Flow in Swift
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

Android Android Studio API APP Programming Apps blogging CSS DATABASE dsa Features HTML HTML5 installation Interview Questions IOS iPhone javascript Mac objective-c OS Programming quicktips SDK SEO SQL swift Tips & Tricks Tools UI Web Wordpress Xcode

  • SOLID Principles of Software Design
  • Latest features in IOS 16
  • How can R language be used for data analysis?
  • Guide To WordPress Coding Standards
  • What is Algorithm?

©mycodetips.com