-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfailable initializers, failable initializers for enumerations, override failable initializers
166 lines (134 loc) · 4 KB
/
failable initializers, failable initializers for enumerations, override failable initializers
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
// failable initializers
// failable initializers are implemented for numeric type conversions. To ensure conversion between numeric types maintains the value exactly, use the init(exactly:) initializer. If the type conversion can’t maintain the value, the initializer fails.
var wholeNumber = 1234.0
var pi = 3.14159
if let numberConversion = Int(exactly: wholeNumber){
print("\(numberConversion) is maintain the value for \(wholeNumber)")
}
let newValue = Int(exactly: pi)
// the newValue is of type Int?, not Int
if newValue == nil{
print("\(newValue ?? 0) is not maintain the value for \(pi) conversion")
}
struct Animal{
var species: String
init?(species: String){
if species.isEmpty {return nil}
self.species = species
}
}
var someHorse = Animal(species: "Horse")
if let horse = someHorse{
print("an animal initialized with name of \(horse.species)")
}
// failable initializers for enumerations
enum Temprature{
case celsius, kelvin, fahrenheit
init?(symbol: Character){
switch symbol {
case "c":
self = .celsius
case "k":
self = .kelvin
case "f":
self = .fahrenheit
default:
return nil
}
}
}
var celsiusInit = Temprature(symbol: "c")
if celsiusInit != nil{
print("initialization of temprature unit in celsius is succeed")
}
var unknownUnit = Temprature(symbol: "x")
if unknownUnit == nil{
print("this is not a defined unit, so initialization is failed")
}
// failable initializers for enumerations with raw values
enum tempratureUnit: Character{
case celsius = "c", kelvin = "k", fahrenheit = "f"
}
var fahrenheitUnit = tempratureUnit(rawValue: "f")
if fahrenheitUnit != nil{
print("defined unit f for fahrenheit is initialized")
}
var Undefined = tempratureUnit(rawValue: "x")
if Undefined == nil{
print("undefined unit x is not initialized")
}
//example:
class Resolute{
var navigationSystem: Bool
init(status: Bool){
if status != true {print("navigation system not responding")}
self.navigationSystem = status
}
}
func lifeSupportSystem(){
print("Alert! life support system execution started.")
}
class PilotModule: Resolute{
var engine: Bool
init(engineStatus: Bool, navigationStatus: Bool){
if engineStatus == false {lifeSupportSystem()
}
self.engine = engineStatus
super.init(status: navigationStatus)
}
}
var moduleOne = PilotModule(engineStatus: false,navigationStatus: true)
// propagation of initialization failure
class vehicle{
var currentSpeed: Int
init?(speed: Int){
if speed == 0 {return nil}
self.currentSpeed = speed
}
}
class Tesla: vehicle{
var batteryStatus: Int
init?(battery: Int, speed: Int){
if battery < 1 {return nil}
self.batteryStatus = battery
super.init(speed: speed)
}
}
if let teslaOne = Tesla(battery: 67, speed: 60){
print("the current battery status of tesla one is: \(teslaOne.batteryStatus) percent, and speed is: \(teslaOne.currentSpeed) KMPH")
}
if let teslaTwo = Tesla(battery: 0, speed: 60){
print("the current battery status of tesla two is: \(teslaTwo.batteryStatus) percent, and speed is: \(teslaTwo.currentSpeed) KMPH")
}
else{print("tesla is not runnning, battery is empty")}
//override failable initializers
// the only to overirde a failable initializer is force unwrapping
class Document{
var name: String?
init() {}
init?(name: String){
if name.isEmpty { return nil }
self.name = name
}
}
class RenameDocument: Document{
override init(){
super.init()
self.name = "Untitled"
}
override init(name: String){
super.init()
if name.isEmpty {
self.name = "Untitled"
}
else{
self.name = name
}
}
}
// let's resolve runtime error
class resolve: Document{
override init(){
super.init(name: "document")!
}
}