-
Notifications
You must be signed in to change notification settings - Fork 10
/
Contents.swift
116 lines (84 loc) · 3.08 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import UIKit
// Sets: an unordered collection whose elements are unique
//====================================================
// initializing and populating a set
//====================================================
// protocols: Hashable, Sequence, Collection
var accountNumbers: Set<Int> = [] // use type annotation
var accountNumbers2 = Set<Int>() // use set initialization
var accountNumbers3: Set<Int> = [37198473, 94275942789, 95727589278] // using a literal collection
// heterogeneous array
var mixedArr: [Any] = [45253, "Tiff", false]
for element in mixedArr {
if let num = element as? Int { // typecasting - optional downcasting as?, forced downcasting as!
print("num is \(num)")
}
if let str = element as? String {
print("string is \(str)")
}
if let bool = element as? Bool {
print("bool value is \(bool)")
}
}
//====================================================
// accessing values in a set
//====================================================
// isEmpty
if accountNumbers.isEmpty {
print("accountNumbers is empty")
}
// count
print("There are \(accountNumbers3.count) elements in accountNumbers3")
// converting array to a set
// removes any duplicates from the array since elements in a set has to be unique
let colors = Set(["blue", "white", "red", "yellow", "white"])
print(colors)
//====================================================
// using contains to find an element in a set
//====================================================
var fellows: Set<String> = ["Christian", "Gregg", "Maitree", "Joshua"]
for fellow in fellows {
print(fellow)
}
if fellows.contains("Christian") {
print("found fellow")
}
//====================================================
// removing and inserting in a set
//====================================================
let inserted = fellows.insert("Nancy")
// returns a tuple indicating value insert status
// (inserted: true, memberAfterInsert: "Nancy")
print(inserted)
fellows.insert("Paul")
print(fellows)
// ["Christian", "Joshua", "Gregg", "Nancy", "Maitree", "Paul"]
fellows.remove("Paul")
print(fellows)
// ["Joshua", "Nancy", "Christian", "Gregg", "Maitree"]
//====================================================
// performing set operations
//====================================================
let a: Set<Int> = [1, 2, 3, 4, 5, 6]
let b: Set<Int> = [5, 6, 7, 8]
// intersection
let intersection = a.intersection(b).sorted() // ascending is the default order of sorted()
print(intersection) // [5,6]
// symmetricDifference
let symmetricDifference = a.symmetricDifference(b).sorted()
print(symmetricDifference) // [1, 2, 3, 4, 7, 8]
// union
let union = a.union(b).sorted()
print(union) // [1, 2, 3, 4, 5, 6, 7, 8]
// subtracting
let subtracting = a.subtracting(b).sorted()
print(subtracting)
let houseAnimals: Set = ["🐶", "🐱"]
let farmAnimals: Set = ["🐮", "🐔", "🐑", "🐶", "🐱"]
let cityAnimals: Set = ["🐦", "🐭"]
houseAnimals.isSubset(of: farmAnimals)
// true
farmAnimals.isSuperset(of: houseAnimals)
// true
farmAnimals.isDisjoint(with: cityAnimals)
// true