-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdemo.swift
246 lines (184 loc) · 5.14 KB
/
demo.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
//MARK:- Inheritance
// Base Class
class Vehicle {
var currentSpeed = 0.0
var description: String {
return "traveling at \(currentSpeed) miles per hour"
}
func makeNoise() {
// do nothing - an arbitrary vehicle doesn't necessarily make a noise
}
}
class Bicycle: Vehicle {
var hasBasket = false
}
class Tandem: Bicycle {
var currentNumberOfPassengers = 0
}
// Overriding
// Overriding methods
class Train: Vehicle {
override func makeNoise() {
print("Choo Choo")
}
}
//Overriding Properties
class Car: Vehicle {
var gear = 1
override var description: String {
return super.description + " in gear \(gear)"
}
}
// Overriding Property Observers
class AutomaticCar: Car {
override var currentSpeed: Double {
didSet{
gear = Int(currentSpeed / 10.0) + 1
}
}
}
//MARK:- Extensions
// Computed Properties
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
// Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.
// Initializers
struct Rect {
var origin = Point()
var size = Size()
}
extension Rect {
init(center: Point, size: Size) {
let originX = center.x - (size.width / 2)
let originY = center.y - (size.height / 2)
self.init(origin: Point(x: originX, y: originY), size: size)
}
}
//MARK:- Protocols
// Here's an example of a protocol with a single instance property requirement:
protocol FullyNamed {
var fullName: String { get }
}
struct Person: FullyNamed {
var fullName: String
}
class Starship: FullyNamed {
var prefix: String?
var name: String
init(name: String, prefix: String? = nil) {
self.name = name
self.prefix = prefix
}
var fullName: String {
return ((prefix != nil ? prefix! + " " : "") + name)
}
}
// Mutating Method Requirements
protocol Togglable {
mutating func toggle()
}
enum OnOffSwitch: Togglable {
case off, on
mutating func toggle() {
switch self {
case .off:
self = .on
case .on:
self = .off
}
}
}
// Protocol Composition
protocol Named {
var name: String { get }
}
protocol Aged {
var age: Int { get }
}
struct Person2: Named, Aged {
var name: String
var age: Int
}
// Optional Protocol Requirements
@objc protocol CounterDataSource {
@objc optional func increment(forCount count: Int) -> Int
@objc optional var fixedIncrement: Int { get }
}
class Counter {
var count = 0
var dataSource: CounterDataSource?
func increment() {
if let amount = dataSource?.increment?(forCount: count) {
count += amount
} else if let amount = dataSource?.fixedIncrement {
count += amount
}
}
}
class ThreeSource: NSObject, CounterDataSource {
let fixedIncrement = 3
}
@objc class TowardsZeroSource: NSObject, CounterDataSource {
func increment(forCount count: Int) -> Int {
if count == 0 {
return 0
} else if count < 0 {
return 1
} else {
return -1
}
}
}
// MARK:- Generic Types
// Non-generic version of a Stack first
struct IntStack {
var items = [Int]()
mutating func push(_ item: Int) {
items.append(item)
}
mutating func pop() -> Int {
return items.removeLast()
}
}
// Now a Generic version
struct Stack<Element> {
var items = [Element]()
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
}
// Extending a Generic Type
extension Stack {
var topItem: Element? {
return items.isEmpty ? nil : items[items.count - 1]
}
}
// MARK: - Access levels
// Default Access level - Internal
class SomeImplicitlyInternalClass {} // implicitly internal
let someImplicitlyInternalConstant = 0 // implicitly internal
public class AnotherPublicClass { // explicitly public class
public var somePublicProperty = 0 // explicitly public class member
var someInternalProperty = 0 // implicitly internal class member
private func somePrivateMethod() {} // explicitly private class member
}
class AnotherInternalClass { // implicitly internal class
var someInternalProperty = 0 // implicitly internal class member
private func somePrivateMethod() {} // explicitly private class member
}
fileprivate class AnotherFilePrivateClass { // explicitly file-private class
func someFilePrivateMethod() {} // inplicitly file-private method
private func somePrivateMethod() {} // explicitly file-private method
}
private class AnotherPrivateClass { // explicitly private class
var somePrivateProperty = 0 // implicitly private class member
func somePrivateMethod() {} // implicitly private class member
}