forked from palaniraja/swiftuml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkitchensink.swift
346 lines (262 loc) · 7.91 KB
/
kitchensink.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//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 Size {
var width = 0.0, height = 0.0
}
struct Point {
var x = 0.0, y = 0.0
}
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
// Property Requirements
// Property requirements are always declared as variable properties, prefixed with the var keyword.
protocol SomeProtocol {
var mustBeSettable: Int { get set }
var doesNotNeedToBeSettable: Int { get }
}
// Always prefix type property requirements with the static keyword when you define them in a protocol. This is true even though type property requirements can be prefixed with the static or class keyword when implemented by a class:
protocol AnotherProtocol {
static var someTypeProperty: Int { get set }
}
// 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)
}
}
protocol RandomNumberGenerator {
func random() -> Double
}
// linear congruential generator:
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy:m))
return lastRandom / m
}
}
// 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
// Access Control Syntax
public class SomePublicClass {}
internal class SomeInternalClass {}
fileprivate class SomeFilePrivateClass {}
private class SomePrivateClass {}
public var somePublicVariable = 0
internal let someInternalConstant = 0
fileprivate func someFilePrivateFunction() {}
private func somePriviateFunction() {}
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
}
// Tuple Types
// Tuples don't have a way to explicitly set their access level. Instead the tuples access level is set from the most restrictive access level of its components
// Function Types
// The access level of a function is set as the most restrictive access level of the function arguments and return type. You also must explicitly set the access level of the function type if it doesn't match the default
/*
func someFunction() -> (SomeInternalClass, SomePrivateClass) {
// function implementation goes here
}
The function above won't compile because the SomePrivateClass sets the function type to private which is not explicitly set.
*/
private func someFunction() -> (SomeInternalClass, SomePrivateClass) {
// function implementation goes here
return (SomeInternalClass(), SomePrivateClass())
}
// Enumeration Types
// The individual cases of an enumeration have the same access level as the enum
private enum CompassPoint {
case north
case south
case east
case west
}
// Subclasses
// A subclass cannot have a higher access level than its superclass.
// An override can make an inherited class member more accessible than its superclass.
public class A {
fileprivate func someMethod() {}
}
internal class B: A {
override internal func someMethod() {}
}
public class C {
fileprivate func someMethod() {}
}
internal class D: C {
override internal func someMethod() {
super.someMethod()
}
}