-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperties_Part2.swift
137 lines (118 loc) · 2.69 KB
/
Properties_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
struct Structure {
static var name = "Sturcture"
static var value = 1
static var age = 18
static var minValue : Int {
get{
return value
}
set{
value = newValue
}
}
var newAge: Int{
get{
return Structure.age
}
set{
if newValue > Structure.age
{
Structure.age = newValue
}
}
}
var function: Void
{
print(Structure.name, Structure.value, Structure.age)
}
}
var struct1 = Structure()
print("Structure")
struct1.function
struct1.newAge = 22
print(struct1.newAge)
print(Structure.age)
var struct2 = Structure()
print(struct2.newAge)
Structure.minValue = -1
print(Structure.minValue)
print(Structure.value)
//----------------------------------------------------------------------------
class Class {
static var name = "Class"
static var value = 1
//class var age = 18 -- error-- class keyword isn’t supported with stored properties.
static var age = 18
static var minValue : Int {
get{
return value
}
set{
value = newValue
}
}
var newAge: Int{
get{
return Class.age
}
set{
if newValue > Class.age
{
Class.age = newValue
}
}
}
class var num: Int {
return 0
}
var function: Void
{
print(Class.name, Class.value, Class.age)
}
}
class SubClass: Class{
override class var num: Int
{
return 7
}
//override static var minValue: Int //-- error -- Static Type Properties can’t be overridden in the subclass. The keyword class is used on the computed properties in this case.
}
var class1 = Class()
print("class")
class1.function
class1.newAge = 22
print(class1.newAge)
print(Class.age)
var class2 = Class()
print(class2.newAge)
Class.minValue = -1
print(Class.minValue)
print(Class.value)
print(Class.num)
print(SubClass.num)// overriding the parent class
//----------------------------------------------------------------
enum Enumeration {
//var stored = "Enumerations"// error - enums not contain stored properties
case lenovo
case samsung
var computed: String {
switch self{
case .lenovo:
return "LEN"
case .samsung:
return "SAM"
}
}
}
var enum1 = Enumeration.lenovo
print(enum1.computed)
enum Enum: Int{
static var stored = "Enum"
case app = 1
static var computed: String
{
return "This is \(Enum.stored) and the app value is \(app.rawValue)"
}
}
print(Enum.app.rawValue)
print(Enum.computed)