-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_8_Structures_Part_One.swift
126 lines (85 loc) · 2.63 KB
/
Day_8_Structures_Part_One.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
/*
Day 8 : Structures Part One
- Creating your own structs
- Computed properties
- ProperCreating your own structsty observers
- Methods
- Mutating methods
- Properties and methods of strings
- Properties and methods of arrays
*/
// -------------------- Creating your own structs ------------------------------------
// Create my own structs
struct Sport {
var name: String
}
var tennis = Sport(name: "Tennis")
// You can change the properties' value too!
tennis.name = "Lawn Tennis"
print(tennis.name)
// -------------------- Computed properties ------------------------------------------
// Computed Properties
struct Sport2 {
var name: String
var isOlympicSport: Bool
var olympicStatus: String {
if isOlympicSport {
return "\(name) is an Olympic sport"
} else {
return "\(name) is not an Olympic sport"
}
}
}
let chessBoxing = Sport2(name: "Chessboxing", isOlympicSport: false)
print(chessBoxing.olympicStatus)
// -------------------- ProperCreating your own structsty observers-------------------
// Property Observer
// Change value every time it has a change
struct Progress {
var task: String
var amount: Int {
didSet {
print("\(task) is now \(amount)% complete")
}
}
}
var progress = Progress(task: "Loading data", amount: 0)
progress.amount = 30
progress.amount = 50
progress.amount = 100
// -------------------- Methods ------------------------------------------------------
// Methods = action of struct (what you want that struct to do)
struct City {
var population: Int
func collectTexus() -> Int {
return population * 1000
}
}
let london = City(population: 9_000_000)
print(london.collectTexus())
// -------------------- Mutating methods ---------------------------------------------
// Mutating function
struct Person {
var name: String = "Mark"
// "name" can not be changed since it is a constant
mutating func makeAnonymous() {
name = "Anonymous"
}
}
// -------------------- Properties and methods of strings ----------------------------
// Properties and methods of strings
// String; they have their own methods and properties
let string = "Do or do not, there is no try."
print(string.count)
print(string.hasPrefix("Do"))
print(string.uppercased())
print(string.sorted())
// -------------------- Properties and methods of arrays -----------------------------
// Properties and methods of arrays
var toys = ["Woody"]
print(toys.count)
toys.append("Buzz")
toys.firstIndex(of: "Buzz")
print(toys)
print(toys.sorted())
toys.remove(at: 0)