-
Notifications
You must be signed in to change notification settings - Fork 10
/
Contents.swift
249 lines (198 loc) · 7.04 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import UIKit
// classes: building block that enable us to create complex "objects" in our program. Those objects are created using properties and methods.
//=======================================================
// class syntax
//=======================================================
class MyFirstClass {
}
// Day not has added functionality to count how many
// cases it has e.g here it is 3
// enum Day conforms (adds more functionality) to CaseIterable
enum Day: CaseIterable {
case monday, tueday, wednesday
}
Day.allCases.count
//=======================================================
// inheritance - ONLY classes can inherit from other classes
//=======================================================
class Person {
var name = "Jane Doe" // stored property
var age = 21
// instance methods
func info() {
print("This person name is \(name) and age is \(age)")
}
}
// Fellow inherits from Person
// Fellow is a subclass of Person
// Person is the parent or superclass of Fellow
// In Swift we only have single inheritance, meaning you can ONLY
// inherit from one parent
// Object-oriented programming consists on those concepts:
// - inheritance
// - encapsulation: use private access modifiers on your properties and access them externally through methods
// - polymorphism - changing the form of an object
// The class Fellow inherits from the Person class above
class Fellow: Person {
// DOES NOT COMPILE BECAUSE PARENT HAS THE SAME METHOD NAME
// SO FELLOW NEEDS TO USE the override keyword
// func info() {
//
// }
override func info() {
print("\(name) is a fellow at Pursuit and is \(age) years old")
}
}
class Employee: Person {
// properties
var position = "iOS developer"
var salary = 85_000
// instance methods
override func info() {
print("\(position) is making $\(salary)")
}
}
// creating an instance of Fellow()
let brendon = Fellow()
brendon.name = "Brendon"
brendon.age = 31
brendon.info()
// before the Fellow class over rode info() method from the Person class this was the output from the print statement
// "This person name is Brendon and age is 31"
// after over ridding info() in the Fellow class the print statment comes from the overriden method
// "Brendon is a fellow at Pursuit and is 31 years old"
/* DOES NOT COMPILE - STRUCTS CANNOT INHERIT FROM OTHER STRUCTS
struct Artist { } - If Artist is a protocol then code will compile
struct PopStar: Artist { }
*/
//=======================================================
// polymorphism - changing form on an object
//=======================================================
// instance of a Person
let tom = Person()
tom.name = "Tom"
// instance of an Employee
let john = Employee()
john.name = "John"
// instance of a Fellow
let bienbenido = Fellow()
bienbenido.name = "Bienbenido"
// an array of instances from above
let people = [tom, john, bienbenido]
// if we were to loop over the people array what for example would be the info() message for bienbenido
for person in people {
person.info()
}
//=======================================================
// struct are value-types - meaning assigning a new variable
// that new variable gets a copy of
// the original. Changing the copy
// does not change the original
//
// classes are reference types - multiple instances point to
// the same object meaning object b can
// mutate object a
//=======================================================
// creating an instance of Fellow() named anna
let anna = Fellow()
anna.name = "Anna"
print(anna.name) // Anna
// assinging luba the anna object
let luba = anna // both anna and luba are pointing to the same object
print(luba.name) // Anna
luba.name = "Luba"
print(luba.name) // Luba
// what is anna's name if printed
print(anna.name) // Luba
//=======================================================
// Swift provides a way to check for object equality of
// identity using ===
//=======================================================
if anna === luba { // ONLY available on classes
print("both object are pointing to the same reference")
}
//=======================================================
// introduction to initializer in classes
//=======================================================
struct Dog {
var breed: String
var age: Int
}
// creating an instance of Dog using the memberwise initializer
// of the Dog struct, ONLY structs provide memberwise initialzers
var frenchBulldog = Dog(breed: "French Bulldog", age: 7)
// compiler error if class does not provide default values
// for ALL properties
class Game {
// properties
var name: String
var noOfPlayers: Int
// initializers
init(name: String, noOfPlayers: Int) {
self.name = name
// basketball.name = "basketball"
self.noOfPlayers = noOfPlayers // 5
// basketball.noOfPlayers = 5
}
// instance method
func info() {
print("\(name) has \(noOfPlayers) players")
}
}
// creating an instance of Game() called basketball
let basketball = Game(name: "Basketball", noOfPlayers: 5)
let football = Game(name: "Football", noOfPlayers: 11)
basketball.info()
// accessing properties
basketball.name = "NBA"
basketball.info()
//=======================================================
// creating instances from a dictionary
//=======================================================
class Playlist {
// properties
var name: String
var trackCount: Int
var description: String
var isTop100: Bool
// initializer
init(name: String,
trackCount: Int,
description: String,
isTop100: Bool){
self.name = name
self.trackCount = trackCount
self.description = description
self.isTop100 = isTop100
}
// instance method
func info() {
print("\(name) contains \(trackCount) tracks. \(description)")
if isTop100 {
print("\(name) is on the top 100 Spotify list")
}
}
}
let playlistDict: [String: Any] = ["name": "Best Pop Songs 2019",
"trackCount": 128,
"description": "Awesome playlist",
"isTop100": true
]
// write a function that takes a dictionary of type [String: Any] and returns an optional Playlist?
func getPlaylist(dict: [String: Any]) -> Playlist {
// accessing the name key in dict["name"]
// used typecasting of Any type to String type using optional downcasting as?
// used nil-coalescing to unwrap optional String? to String
let name = dict["name"] as? String ?? "no name available"
let trackCount = dict["trackCount"] as? Int ?? 0
let description = dict["description"] as? String ?? "no description is availble"
let isTop100 = dict["isTop100"] as? Bool ?? false
let playlist = Playlist(name: name,
trackCount: trackCount,
description: description,
isTop100: isTop100)
return playlist
}
// calling the getPlalist function which return a Playlist
let playlist = getPlaylist(dict: playlistDict)
playlist.info()