-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitial values for wrapped properties, property wrappers
125 lines (105 loc) · 3.53 KB
/
Initial values for wrapped properties, property wrappers
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
// property observers (willSet & didSet)
class minibanken{
var initialValue: Int = 0{
willSet(newValue){
print("the new value is: \(newValue)")
}
didSet{
if initialValue > oldValue{
print("The didSet observer is called after the value of initialValue is updated: \(initialValue)")
print("the old value was: \(oldValue)")
print("the total value is now: \(initialValue + oldValue)")
}
}
}
}
var deposit = minibanken()
deposit.initialValue = 200
print("\nthe did is not invoke if value wasn't updated",deposit.initialValue = 200)
print(" \n ", deposit.initialValue = 300)
// property wrappers
@propertyWrapper
struct thirtyOrLess{
private var number = 0
var wrappedValue: Int{
get {return number}
set {number = min(newValue, 30 - 4)}
}
}
struct inputNumber{
@thirtyOrLess var presentInJune: Int
@thirtyOrLess var presentInApril: Int
// @thirtyOrLess written as a attribute
}
var present = inputNumber()
present.presentInJune = 20
present.presentInApril = 31
print("presents in june are: \(present.presentInJune)")
print("presents in june are: \(present.presentInApril)")
// initial values for wrapped properties
@propertyWrapper
struct presentCount{
private var maximumDays = 0
private var number = 0
var wrappedValue: Int{
get {return number}
set {number = min(newValue, maximumDays)}
}
init(){
maximumDays = 27
number = 0
}
init(wrappedValue: Int){
maximumDays = 24
number = min(wrappedValue, maximumDays)
}
init(wrappedValue: Int, maximumDays: Int){
self.maximumDays = maximumDays
number = min(wrappedValue, maximumDays)
}
}
struct thirtyDayMonths{
@presentCount(wrappedValue: 0, maximumDays: 26 ) var june: Int
@presentCount(wrappedValue: 0, maximumDays: 26 ) var november: Int
@presentCount(wrappedValue: 0, maximumDays: 26 ) var april: Int
@presentCount(wrappedValue: 0, maximumDays: 26 ) var september: Int
}
var fourMonths = thirtyDayMonths()
fourMonths.june = 15
fourMonths.november = 20
fourMonths.april = 25
fourMonths.september = 10
print("working days of june was: \(fourMonths.june)")
print("working days of june was: \(fourMonths.november)")
print("working days of june was: \(fourMonths.april)")
print("working days of june was: \(fourMonths.september)")
struct FebruaryMonth{
@presentCount(wrappedValue: 20) var february: Int
}
var februaryMonth = FebruaryMonth()
februaryMonth.february = 25
print("\nworking days of february was: \(februaryMonth.february)")
struct Day31Months{
@presentCount var january: Int
@presentCount var march: Int
@presentCount var may: Int
@presentCount var july: Int
@presentCount var august: Int
@presentCount var october: Int
@presentCount var december: Int
}
var day31Months = Day31Months()
day31Months.january = 30
day31Months.march = 13
day31Months.may = 14
day31Months.july = 15
day31Months.august = 16
day31Months.october = 17
day31Months.december = 18
print("\n working days of january was: \(day31Months.january) ")
print("working days of march was: \(day31Months.march) ")
print("working days of may was: \(day31Months.may) ")
print("working days of july was: \(day31Months.july) ")
print("working days of august was: \(day31Months.august) ")
print("working days of october was: \(day31Months.october) ")
print("working days of december was: \(day31Months.december) ")