-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
214 lines (167 loc) · 4.9 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//Objects and classes
class Shape{
var numberOfSides: Int = 0
var name: String
//Constructor
init(name: String, numberOfSides: Int){
self.name = name
self.numberOfSides = numberOfSides
}
func simpleDescription() -> String{
return "A shape with \(numberOfSides) sides."
}
//Destructor
deinit {
print("i am dead now!")
}
}
//Instance of class (object)
var square: Shape? = Shape(name: "square", numberOfSides: 4)
if let shape = square{
print(shape.simpleDescription())
}
else{
print("This is nil")
}
square = nil //this will call the deinit destructor
//Lets assume
class ShapeParent{
var numberOfSides: Int = 0
var name: String
init(name: String){
self.name = name
}
func simpleDescription() -> String{
return "A shape with \(numberOfSides)"
}
}
class Square: ShapeParent{
var sideLength: Double
init(sideLength: Double, name: String){
self.sideLength = sideLength
super.init(name: name)
super.numberOfSides = 4
}
func area() -> Double{
return sideLength * sideLength
}
override func simpleDescription() -> String{
return "A square with sides of length \(sideLength)"
}
}
let sq1 = Square(sideLength: 5.2, name: "my test square")
sq1.simpleDescription()
//Getter setter property demonstrate
class EquiLateralTriangle: ShapeParent{
var sideLength: Double = 0.0
init(sideLength: Double, name: String){
self.sideLength = sideLength
super.init(name:name)
super.numberOfSides = 3
}
var perimeter: Double{
get{
return 3.0 * sideLength
}
set(newValue){
sideLength = newValue / 3.0
}
}
}
var triangle: EquiLateralTriangle = EquiLateralTriangle(sideLength: 12, name: "my test triangle")
print(triangle.perimeter) //Will call the getter of the perimeter member
triangle.perimeter = 9 //This is the setter function called
print(triangle.sideLength)
print(triangle.perimeter)
//Property observers
/*
observers observe and respond to changes in a property's value. Property observers are called every time a property's value is set even if new value is the same as property's current value
*/
class StepCounter{
var totalSteps: Int = 0{
willSet(newTotalSteps){
print("About to set totalsteps to \(newTotalSteps)")
}
didSet{
if totalSteps > oldValue{
print("Added \(totalSteps - oldValue) steps")
}
}
}
}
var stepCounter = StepCounter()
var s1 = stepCounter
stepCounter.totalSteps = 200
stepCounter.totalSteps = 1000
//As you see in the output as we keep changing/assigning the value two print statements come. the first is the willSet (about to set) then the didSet just after setting the value
//If you assign an object to another object then the new variable will point to the original object
//Example:
//stepCounter is an object of StepCounter another object is made s1
//s1 = stepCounter
//Now after changiong the values in stepCounter
print(s1.totalSteps) //will be 1000 Refrence types
//PROPERTIES
class Person{
var name: String = ""
init(name: String){
self.name = name
}
}
class Employee: Person{
var salary: Int = 0
var role: String = ""
init(salary: Int, role: String, name: String){
self.salary = salary
self.role = role
super.init(name: name)
}
func doWork(){
print("Hi my name is \(name) and I am doing work")
salary+=1
}
}
class Manager: Employee{
var teamSize = 0
var bonus: Int{ //Computed property
return teamSize * 1000
}
init(salary: Int, role: String, name: String, teamSize: Int){
super.init(salary: salary, role : role, name: name)
self.teamSize = teamSize
}
override func doWork(){
super.doWork()
print("I am managing people")
salary+=2
}
}
var raghav = Manager(salary: 12, role: "CEO", name: "Raghav", teamSize: 390)
print(raghav.name)
//Designated and convenience initializers
//initializer should also make sure that the members are filled properly
//Designated initilizers ensure that the object is ready to be used
class PersonTest{
var name: String
var netWorth: Int?//By default nil
var gender: String!//By defualt nil Already unwrapped
init(){
name = "None"
}
}
let a = PersonTest()
print(a.name)
//Conveniance initializers
class PersonTest2{
var name: String
var netWorth: Int?//By default nil
var gender: String!//By defualt nil Already unwrapped
init(){
name = "None"
}
convenience init(_gender: String, _networth: Int){
self.init()
self.gender = gender
self.netWorth = netWorth
}
}
let n = PersonTest2(_gender: "female", _networth: 100000)