-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
156 lines (126 loc) · 3.86 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//Enumerations:
/*
special case class
Represents a group of constants
(unchangeable variales)
*/
/*
The major difference between enums and classes is that enums are value types while classes are refrence types
If you assign a class type obj to another they both point to the same instance of the class as demonstrated above in classes.
No new copy is created
In enums and structs a new copy is made (value type)
*/
//Syntax
enum CompassPoint{
case north
case south
case east
case west
}
var hello = CompassPoint.north
//values defined in an enumeration are its enumeration cases.
//Unlike C cases dont have an integer value set by dfault.
//Each case are values in their own right
print(hello) //will print north
hello = .east
//Matching enum values with switch
var directionToHead = CompassPoint.south
switch directionToHead {
case .north:
print("GO NORTH")
case .south:
print("GO SOUTH")
case .west:
print("GO WEST")
case .east:
print("GO EAST")
default: //Here this isnt required as we have put in all the possible cases above. Making it exhaustive
print("ERROR")
}
//Iterating over enumeration cases
enum Beverage: CaseIterable{
case coffee, chai, juice, aamras
}
let numberOfChoices = Beverage.allCases.count
for i in Beverage.allCases{
print(i)
}
//Associated Values
//We can also set a const or variable to enum.case and check the value later
enum Barcode{
case upc(Int, Int, Int, Int)
case qrCode(String)
}
var productBarcode = Barcode.upc(8,8059,5122,3)
print(productBarcode)
productBarcode = .qrCode("ABDDD")
//now this variable stores a qr code
//earlier it stored a UPC barcode
switch productBarcode{
case .upc(let numberSys, let manufacturer, let product, let check):
print("UPC:\(numberSys) \(manufacturer) \(product) \(check)")
case .qrCode(let productCode):
print("QR code: \(productCode)")
}
//pls note let and var will both work the same. No difference
//Raw values
enum ASCIIControlCharacter: Character{
case tab = "\t"
case lineFeed = "\n"
case carriageReturn = "\r"
}
//the raw values for an enumeration are defined to be a type of character
//Raw values can be strings characters or any number
//Implicit assigning of raw values
enum Planet: Int{
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
//Just like in C the rest of the planets got successive number
}
//for strings
enum Compass: String{
case north
case south
case east
case west
}
let myDirection: String = Compass.west.rawValue
print(myDirection) //=>Now myDirection contains a string
//Essentially the label name north or south becomes the raw value
//implicit value for each case is the text of that case's name
//Initializing from a Raw value
let possiblePlanet: Planet? = Planet(rawValue: 7)
//This is now Uranus
//THis is an optional
//so
if let planet = possiblePlanet{
print(planet)
}
else{
print("Doesn't exist")
}
//recursive enumerations
/*
another instance of the enumeration as the associated value for one or more of the enumeration cases.
*/
enum ArithmeticExpression{
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case subtraction(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
func evaluate(_ expression: ArithmeticExpression) -> Int{
switch expression{
case let .number(value):
return value
case let .addition(left, right):
return evaluate(left) + evaluate(right)
case let .multiplication(left, right):
return evaluate(left) * evaluate(right)
case let .subtraction(left, right):
return evaluate(left) - evaluate(right)
}
}
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let addition = ArithmeticExpression.addition(four, five)
print(evaluate(addition))