Skip to content

Latest commit

 

History

History
112 lines (63 loc) · 1.46 KB

File metadata and controls

112 lines (63 loc) · 1.46 KB

Basics | Arrays | Colors | Date and Time | Dictionaries | Sets | Sorting | Strings | Functional Swift

Swift Sets

Two ways to create

var techTickerSet = Set<String>() // Empty

var financialTickerSet = Set<String>(["GS", "MS", "BAC"])

Add values to set

for t in ["AAPL", "MSFT", "INTC", "GOOG"] {

    techTickerSet.insert(t)

}

Insert Value if it is not in the set

if !techTickerSet.contains("BBRY") {
    techTickerSet.insert("BBRY")
}
let nameSet = Set(["Apple", "Microsoft", "Intel", "Alphabet"])

nameSet.first

techTickerSet.count

techTickerSet.contains("AAPL")

Number of elements in Set/Size

allTickers.count

Is Set Empty

allTickers.isEmpty

Set Contains Value

techTickerSet.contains("BBRY")

Remove Element

techTickerSet.remove("BBRY")

Disjoint Set

techTickerSet.isDisjoint(with: financialTickerSet)

Set Union

var allTickers = techTickerSet.union(financialTickerSet)

let all = set1 + set2 + set3

Set Intersection

let intersect = aSet1.intersection(aSet2)

Superset and Subset

allTickers.isSuperset(of: techTickerSet)

techTickerSet.isSubset(of: allTickers)

Remove All Elements

allTickers.removeAll()