-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitializer_Part2.swift
177 lines (156 loc) · 3.82 KB
/
Initializer_Part2.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
//A failable initializer creates an optional value of the type it initializes. You write return nil within a failable initializer to indicate a point at which initialization failure can be triggered.
class Item
{
var name: String
init?(name: String)
{
if name.isEmpty
{
return nil
}
self.name = name
}
}
class Offer: Item
{
var minQuantity: Int
init?(name: String, minQuantity: Int)
{
if minQuantity < 2
{
return nil
}// if it is true no more further initialization happens
self.minQuantity = minQuantity
super.init(name: name)
}
}//if you delegate to another initializer that causes initialization to fail, the entire initialization process fails immediately, and no further initialization code is executed.
var class1 = Offer(name: "", minQuantity: 6)
if class1?.name != nil
{
print("Your order is placed: Item name: \(class1!.name) and Quantity: \(class1!.minQuantity)")
}
else{
print("Item name is needed to purchase item, so order is not valid")
}
var class2 = Offer(name: "Shoe", minQuantity: 1)
if class2?.minQuantity != nil
{
print("Your order is placed: Item name: \(class2!.name) and Quantity: \(class2!.minQuantity)")
}
else
{
print("Minimum quantity is 2 or more, so offer is not valid")
}
// required initializer
//Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer
// def (KP) - If I have a subClass classB of classA, if classB doesn't have it's own designated init then it will inherit it's parent's init. But if I declare init for classB then it may or may not have all the inits of it's parent. to avoid this case I can add required.
class MainClass
{
var num1: Int
required init()
{
num1 = 1
}
}
class ChildClass: MainClass
{
var num2: Int
init(num2: Int)
{
self.num2 = num2
}
required init() { // it tells that every subclass should use required
num2 = 2
}
}
var class5 = ChildClass()
class5.num2
class5.num1
class MainClass1
{
var num1: Int
required init()
{
num1 = 1
}
}
class ChildClass1: MainClass1
{
var num2: Int = 2
}
var class8 = ChildClass1()
class8.num2
class8.num1 // You don’t have to provide an explicit implementation of a required initializer if you can satisfy the requirement with an inherited initializer.
// Overriding failable initializer
class Class
{
var name: String
init()
{
name = "Unname"
}
init!(name: String)
{
if name.isEmpty
{
return nil
}
self.name = name
}
}
var class4 = Class(name: "")
if class4?.name == nil{
print("String is nil")
}
else{
print("\(class4!.name)")
}
class Subclass: Class
{
override init(name: String) // ovveriding failable initializer by non failable initializer
{
if name.isEmpty
{
super.init()
}
else{
super.init(name: name)
}
}
}
var class3 = Subclass(name: "")
print(class3.name)
//Setting a Default Property Value with a Closure or Function
struct Box
{
var boundary: [Int] = {
var temp: [Int] = []
for i in 0...3
{
for j in 0...3
{
if j == 0 || j == 3 || i == 0 || i == 3 {
temp.append(1)
}
else
{
temp.append(0)
}
}
}
return temp
}()
func display() -> Void
{
for i in 0...3
{
for j in 0...3
{
print(boundary[i * 4 + j], terminator: " ")
}
print()
}
}
}
var struct1 = Box()
struct1.display()