-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaining on methods with optional return values, linking multiple levels of chaining
131 lines (109 loc) · 3 KB
/
chaining on methods with optional return values, linking multiple levels of chaining
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
// model classes for optional chaining
class Person{
var residence: Residence?
}
class Residence{
var rooms: [Room] = []
var total: Int{
return rooms.count
}
subscript(i: Int) -> Room{
get{
return rooms[i]
}
set{
rooms[i] = newValue
}
}
func printTotal(){
print("total number of rooms are: \(total)")
}
var address: Address?
}
class Room{
var name: String
init(name: String) {self.name = name}
}
class Address{
var number: String?
var street: String?
var buildingName: String?
func identifier() -> String?{
if let number = number, let street = street{
return "\(number) \(street)"
}
else if(buildingName != nil){
return buildingName;
}
else {
return nil
}
}
}
// accessing properties through optional chaining
let arthur = Person()
if let roomCount = arthur.residence?.total{
print("arthur has \(roomCount) rooms in his residence")
}
else{
print("arthue doesn't have any room.")
}
let someAddress = Address()
someAddress.street = "ny"
someAddress.number = "one"
arthur.residence?.address = someAddress
func createAddress() -> Address{
let someAddress = Address()
someAddress.street = "ny"
someAddress.number = "one"
return someAddress
}
arthur.residence?.address = createAddress()
// calling methods through optional chaining
if arthur.residence?.printTotal() != nil{
print("it was possible to print total rooms")
}
else{
print("print rooms isn't possible yet")
}
if (arthur.residence?.address = someAddress) != nil{
print("it was possible to set the address")
}
else{
print("isn't possible to set address")
}
// accessing subscripts through optional chaining
arthur.residence?[0] = Room(name: "haveli")
var finnsHouse = Residence()
finnsHouse.rooms.append(Room(name: "second room"))
finnsHouse.rooms.append(Room(name: "thirs room"))
arthur.residence = finnsHouse
if let firstRoom = arthur.residence?[0].name{
print("the first room name of arthur is: \(firstRoom)")
}else{
print("can't retrieve the value of room name")
}
// linking multiple levels of chaining
let arthurAddress = Address()
arthurAddress.buildingName = "brainetics"
arthurAddress.street = "silicon valley"
arthurAddress.number = "1234"
arthur.residence?.address = arthurAddress
if let arthurStreet = arthur.residence?.address?.street{
print("street name of arthur residence is: \(arthurStreet)")
}
else{
print("can't retrive the value of street of arthur residence.")
}
// chaining on methods with optional return values
if let building = arthur.residence?.address?.identifier(){
print("identified building name is: \(building)")
}
// performing furthur optional chaining on methods return values
if let beginWith = arthur.residence?.address?.identifier()?.hasPrefix("The"){
if beginWith{
print("arthurs building name begins with \" The \"")
}
} else {
print("can't retrive the value of method")
}