-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARC_Part3.swift
141 lines (101 loc) · 3.14 KB
/
ARC_Part3.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
// ---------Unowned References and Implicitly Unwrapped Optional Properties--------
class Galaxy {
var name: String
var planet: Planet!
//the planet property has a default value of nil, like any other optional, but can be accessed without the need to unwrap its value
init(name: String, planetName: String) {
self.name = name
self.planet = Planet(name: planetName, galaxy: self)
print("galaxy is initialized")
}
deinit {
print("\(name) is deinitialized")
}
}
class Planet {
var name: String
unowned var galaxy: Galaxy
init(name: String, galaxy: Galaxy) {
self.name = name
self.galaxy = galaxy
print("planet is initialized")
}
deinit {
print("\(name) is deinitialized")
}
}
var galaxy1: Galaxy? = Galaxy(name: "MilkyWay", planetName: "Earth")
//planet is initialized
//galaxy is initialized
galaxy1 = nil
//MilkyWay is deinitialized
//Earth is deinitialized
//---------Strong Reference Cycles for Closures-------------
class Contact {
var name: String
var phNo: String
lazy var printDetails: () -> () = {
var name1 = self.name
var no = self.phNo
print("Name: \(name1) \t PhNo: \(no)") // here closure uses self
}
init(name: String, phNo: String) {
self.name = name
self.phNo = phNo
}
deinit {
print("Contact is deinitialized")
}
}
var contact1: Contact? = Contact(name: "Kala", phNo: "123456")
contact1!.printDetails()
contact1 = nil // no deinit msg
// Contact ---> printDetails (strong)
//printDetails ---> contact (Strong) (refering self)
//Strong reference cycle is formed between class and closure (closure are reference types)
//--------capture lists---------
var num1 = 10
var num2 = 10
var capture1 = { [num1] in
print(num1, num2)
}
num1 = 20
num2 = 20
capture1() // 10 20
//Only the value that is captured using square bracket retains the initialize the value
//For reference types
class Number {
var num1 = 10
}
var temp1 = Number()
var temp2 = Number()
var temp3 = Number()
var capture2 = { [temp1, temp2] in
print(temp1.num1, temp2.num1, temp3.num1)
}
temp1.num1 = 20
temp2.num1 = 20
temp3.num1 = 20
capture2() //20 20 20
// All the values are are changed since it is reference type
print("----------Resolving Strong Reference Cycles for Closures---------------")
//Solving the above example of strong reference for closure using unowned
class NewContact {
var name: String
var phNo: String
lazy var printDetails: () -> () = { [weak self] in // (here we can assign unowned or weak that is to be declared in square bracket separated by comma) to avoid strong reference
var name1 = self!.name
var no = self!.phNo
print("Name: \(name1) \t PhNo: \(no)") // here closure uses self
}
init(name: String, phNo: String) {
self.name = name
self.phNo = phNo
}
deinit {
print("Contact is deinitialized")
}
}
var contact2: NewContact? = NewContact(name: "Kala", phNo: "123456")
contact2!.printDetails()
contact2 = nil // contact is deinitialized