-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
116 lines (85 loc) · 2.4 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
// Structures and Classes
// Structures are **value** types
// Classes are **reference** types
// Classes have aditional capabilities:
// - Inheritance enables one class to inherit the characteristics of another.
// - Type casting enables you to check and interpret the type of a class instance at runtime.
// - Deinitializers enable an instance of a class to free up any resources it has assigned.
// - Reference counting allows more than one reference to a class instance.
// Definition syntax
struct Resolution {
var width = 0
var height = 0
}
class VideoMode {
var resolution = Resolution()
var interlaced = false
var frameRate = 0.0
var name: String?
}
// Instances
let someResolution = Resolution()
let someVideoMode = VideoMode()
// Memberwise initializers for structure types
let vga = Resolution(width: 640, height: 480)
// Classes
class Employee {
let hours: Int
init(hours: Int) {
self.hours = hours
}
func summary() {
print("I work \(hours) hours a day")
}
}
class Developer: Employee {
func work() {
print("I'm coding for \(hours) a day")
}
// Override a method
override func summary() {
print("I work in the front of the computer \(hours) hours a day")
}
}
let Iran = Developer(hours: 8)
Iran.work() // I'm coding for 8 hours a day
Iran.summary() // I work in the front of the computer 8 hours a day
// Initializers
class Vehicle {
let isEletric: Bool
init(isEletric: Bool) {
self.isEletric = isEletric
}
}
class Car: Vehicle {
let isConvertible: Bool
// Call super here to initialize also the property from the parent
init(isElectric: Bool, isConvertible: Bool) {
self.isConvertible = isConvertible
super.init(isEletric: isElectric)
}
}
class Actor {
var name = "Nicolas Cage"
}
var actor1 = Actor()
var actor2 = actor1
actor2.name = "Tom Cruise"
actor1.name == actor2.name // true
// Deinitializers
class Site {
let id: Int
init(id: Int) {
self.id = id
print("Site \(id): I've been created")
}
deinit {
print("Site \(id): I've been destroyed")
}
}
for i in 1...2 {
let site = Site(id: 1)
print("Site \(site.id): I'm control")
// Site 1: I've been created / I'm in control / I've been destroyed
// Site 2: I've been created / I'm in control / I've been destroyed
}