Day 13 - 100 Days of Swift
First Review Day
Day 13 is the first review day. You review constants and variables. You review data types and operators and string interpolation. You review arrays and dictionaries. And you review control flow with if statements, switch statements and loops.
You make constants with the let
keyword and variables with the var
keyword. Their type is either inferred from what they are assigned to, or explicitly set with a type annotation:
1
2
3
let lastName = "Bluth" // This is inferred to be a String
var age: Int = 42
Swift has several built in types available for you to use. String
, Int
, Float
, Double
, and Bool
are the common ones:
1
2
3
4
5
6
7
8
9
var firstName: String = "Lindsey"
var numberOfChildren: Int = 1
var publicAge: Float = 29.0
var realAge: Double = 37.0
let madeAHugeMistake: Bool = true
There are also some built in operators. There are mathematical operators, assignment operators, comparison operators, and so on:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Mathematical
1 + 2 // 3
8 - 3 // 5
5 * 9 // 45
6 / 2 // 3
8 % 3 // 2
// Assignment
var num = 5
num += 11 // 16
num -= 2 // 14
num *= 6 // 84
num /= 2 // 42
// Comparison
2 > 5 // false
5 >= 4 // true
3 < 6 // true
4 <= 2 // false
4 == 4 // true
4 != 5 // true
You can “interpolate” objects into a string by putting it in \()
. String interpolation is a fancy way of saying that you can inject a string representation of an object into a string, even though it isn’t necessarily a string itself:
1
2
3
4
let name = "Tobias"
print("\(name): \"I just blue myself\"")
// Tobias: "I just blue myself"
Arrays are a collection of objects of the same type. They are are accessed by index:
1
2
3
4
5
6
7
8
9
10
11
12
var bluths = [
"Michael",
"George Michael",
"George Sr.",
"Lucille",
"Buster",
"Lindsey",
"Gob",
"Maebe"
]
let imprisoned = bluths[2] // "George Sr."
Dictionaries are a collection of key-value pairs. You can access the value by the key:
1
2
3
4
5
6
7
8
9
var placesThereAreMoney: [String: Bool] = [
"Sudden Valley": false,
"Bluth Company": false,
"FakeBlock": false,
"Banana Stand": true
]
// There's always money in the banana stand...
let canGetMoney = placesThereAreMoney["Banana Stand"]
If you only want to run a block of code based on some condition, you can do that with an if
statement. You can check for multiple separate conditions with else if
, and you can provide a default for all other cases with else
:
1
2
3
4
5
6
7
8
9
10
11
12
var careerOver = true
let needsAJob = true
let actingWorksOut = false
if careerOver && needsAJob {
tobiasBecomesAnActor()
careerOver = false
} else if !actingWorksOut {
trainWithCarlWeathers()
} else {
giveUpOnLife()
}
You can loop a fixed number of times with a for
loop, and you can loop until some condition is met with a while
loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for bluth in bluths {
print("\(bluth): \"I made a huge mistake...\"")
}
// Michael: "I made a huge mistake..."
// George Michael: "I made a huge mistake..."
// etc...
let isGeorgeMicahelInLoveWithMaebe = true
while isGeorgeMicahelInLoveWithMaebe {
georgeMichaelTriesToKissHisCousin()
}
// This will loop forever right now.
// Try to avoid that.
Related to the if
statement is the switch
statement. It lets you check multiple cases in a concise way. The compiler will also strictly enforce that all possible cases are covered:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for bluth in bluths {
switch bluth {
case "George Sr.", "Lucille":
print("\(bluth) is in the older generation")
case "Michael", "Lindsey", "Gob", "Buster":
print("\(bluth) is in the middle generation")
case "George Michael", "Maebe":
print("\(bluth) is in the younger generation")
default:
print("I don't know who that person is.")
}
}
// "Michael is in the middle generation"
// "George Michael is in the younger generation"
// etc...
Reflections
Phew. I did not realize how much effort this 100 Days of Swift undertaking would be. Sure. It’s only an hour or two a day. But it is an hour or two every day. And that definitely starts to wear on you. I’m glad he had the foresight to build some rest and review days into the schedule. I can’t say that I learned anything new this time around, but as always, it is good way to refresh my memory on these basics.