0
1
00:00:00,640 --> 00:00:06,150
Guys, welcome to the first of many Swift deep dives. In these sections,
1

2
00:00:06,160 --> 00:00:12,850
I take the opportunity to go into more detail on some of the Swift programming concepts that we've covered
2

3
00:00:13,240 --> 00:00:15,690
in the previous lessons.
3

4
00:00:15,880 --> 00:00:21,690
And the reason is because for all of these things, I'll explain to you as I go along
4

5
00:00:21,700 --> 00:00:27,830
while we're coding. But sometimes some of these topics just deserve a little bit more explanation.
5

6
00:00:27,880 --> 00:00:33,520
Now, for a lot of you who are new to programming, it can seem like there's an overwhelming amount of things
6

7
00:00:33,880 --> 00:00:34,770
that you need to learn.
7

8
00:00:35,290 --> 00:00:38,200
So these deep dives are partly revision
8

9
00:00:38,200 --> 00:00:44,050
to recap some of the things that we've learned in previous lessons. But it also gives me an opportunity
9

10
00:00:44,140 --> 00:00:50,710
to address those of you who are new to programming directly, so that we can really get a deep understanding
10

11
00:00:50,800 --> 00:00:56,410
of the concepts that we're using and we're learning about. The first thing I want to talk about on naming
11

12
00:00:56,410 --> 00:00:57,340
conventions,
12

13
00:00:57,340 --> 00:01:02,080
and previously when we created our IBOutlets and IBActions,
13

14
00:01:02,110 --> 00:01:05,800
we started naming them using camel casing.
14

15
00:01:05,800 --> 00:01:13,000
Now, as I mentioned camel casing in Swift starts out with a lower case letter and every subsequent letter
15

16
00:01:13,150 --> 00:01:15,940
has its first letter capitalized.
16

17
00:01:15,940 --> 00:01:24,250
Now, camel casing is probably the most commonly used way of naming things in programming, but there are
17

18
00:01:24,340 --> 00:01:25,210
other ways, too.
18

19
00:01:25,660 --> 00:01:28,990
So often in web development, you'll see a lot of kebab casing,
19

20
00:01:28,990 --> 00:01:33,700
so words which are all lowercase separated by a dash.
20

21
00:01:33,700 --> 00:01:39,770
And there's also snake case where words are separated by a underscore.
21

22
00:01:39,790 --> 00:01:42,630
Now, some of these things are just convention.
22

23
00:01:42,670 --> 00:01:50,230
So often you'll see file names being named in snake case. You'll see a lot of web development using kebab
23

24
00:01:50,230 --> 00:01:55,590
case and you'll see a lot of application programmers using camel casing.
24

25
00:01:55,600 --> 00:01:58,560
Now, in Swift we don't even have to think about these other two.
25

26
00:01:58,570 --> 00:02:00,790
We're going to stick with camel casing.
26

27
00:02:00,910 --> 00:02:06,270
So whenever you're naming something in code, this is the format that we're going to stick with.
27

28
00:02:06,280 --> 00:02:09,610
Now, the next topic I want to talk about is commenting.
28

29
00:02:09,610 --> 00:02:13,600
We saw how we can use Swift to comment out code.
29

30
00:02:13,600 --> 00:02:22,170
So commenting out means adding two forward slashes which tells Xcode that this line is not code anymore.
30

31
00:02:22,360 --> 00:02:28,900
It's now a comment and you can do this by simply planning on writing a comment.
31

32
00:02:28,900 --> 00:02:34,890
So very often you'll write a comment to explain what the next line of code does to save yourself time
32

33
00:02:34,900 --> 00:02:36,700
the next time you come back to it.
33

34
00:02:36,850 --> 00:02:42,400
But other times you might have an actual line of code. And just by putting two forward slashes in front
34

35
00:02:42,400 --> 00:02:48,430
of it, then you deactivate that line of code but you still keep it around in case you need it in the
35

36
00:02:48,430 --> 00:02:49,660
future.
36

37
00:02:49,660 --> 00:02:55,780
So in order for you to have a play around with this, I recommend heading into Xcode and creating a new
37

38
00:02:55,780 --> 00:02:56,410
playground.
38

39
00:02:56,440 --> 00:03:03,280
So if you head ever to File, New and then select Playground, and then it should create a new playground
39

40
00:03:03,280 --> 00:03:06,610
for you, and then it will ask you what kind of template you want.
40

41
00:03:06,730 --> 00:03:11,340
So we're going to make sure we create an iOS template that is completely blank.
41

42
00:03:11,380 --> 00:03:17,970
So let's go ahead and click next. And then I'm simply going to name it my playground and I'm going to
42

43
00:03:17,970 --> 00:03:20,910
save it in the same place as everything else.
43

44
00:03:20,910 --> 00:03:25,030
Now, once you're done, then you should be able to see this blank playground.
44

45
00:03:25,060 --> 00:03:33,700
And I'm going to expand it to obscure our Dicee project behind. And the playground has three really important
45

46
00:03:33,730 --> 00:03:35,140
areas.
46

47
00:03:35,230 --> 00:03:42,910
You've got the code editor over here. You've got the interpreter which shows you the intermediate steps. And
47

48
00:03:42,910 --> 00:03:49,140
then, finally, you've got the debug area which works really similar to the one we have in Xcode.
48

49
00:03:49,180 --> 00:03:55,900
So the first thing I like to do with playgrounds is to click and hold this button and then change it
49

50
00:03:55,900 --> 00:03:58,680
to automatically run my code.
50

51
00:03:58,690 --> 00:04:05,140
So now every time I type some code, it's going to run it automatically without needing me to do any extra
51

52
00:04:05,200 --> 00:04:06,260
work.
52

53
00:04:06,490 --> 00:04:12,550
And then we're going to delete everything that's currently in our playground and I'm going to create
53

54
00:04:12,580 --> 00:04:13,570
a new comment.
54

55
00:04:13,600 --> 00:04:21,130
This is a comment. Now as I said before, if we had some actual code, let's say, I had a print statement
55

56
00:04:21,190 --> 00:04:23,520
which says, "Hello World".
56

57
00:04:23,530 --> 00:04:25,840
Now once that runs, you're going to see it print
57

58
00:04:25,840 --> 00:04:32,500
down here in the debug console, and then it's also going to show up as an intermediate step to show what
58

59
00:04:32,500 --> 00:04:34,590
this will print as.
59

60
00:04:34,720 --> 00:04:40,210
Now, for now, we can ignore this right-hand side. When we learn a little bit more about Swift programming,
60

61
00:04:40,210 --> 00:04:41,910
it's going to come in more handy.
61

62
00:04:42,220 --> 00:04:48,190
But right now, I'm interested in the fact that this is code which is being executed and printing those
62

63
00:04:48,190 --> 00:04:50,140
words I've told it to print.
63

64
00:04:50,260 --> 00:04:56,590
But if I put two forward slashes in front of it, then it comments out the code, so it deactivates the code.
64

65
00:04:56,680 --> 00:05:00,520
And when it runs again, then it no longer prints that code.
65

66
00:05:00,520 --> 00:05:05,840
And this is now inactive code represented by this gray area.
66

67
00:05:06,010 --> 00:05:14,060
Now, while one option was to do what we did which is just to add those two forward slashes manually, there's
67

68
00:05:14,110 --> 00:05:19,930
a really helpful shortcut that Xcode gives us access to. If you hold down the command key and you're
68

69
00:05:19,960 --> 00:05:22,510
somewhere on the line that you want to comment,
69

70
00:05:22,750 --> 00:05:29,230
then if you press the forward slash key, it will automatically add two forward slashes. And this is because,
70

71
00:05:29,230 --> 00:05:34,510
usually, when you're typing code, you're kind of towards the end of the sentence and then having to touch
71

72
00:05:34,510 --> 00:05:36,160
the mouse and go back to the beginning,
72

73
00:05:36,160 --> 00:05:37,020
it's kind of painful.
73

74
00:05:37,030 --> 00:05:42,760
So it's easier to just hit command, forward slash, and it comments out the entire line of code.
74

75
00:05:43,090 --> 00:05:46,850
And you can toggle it as many times as you want.
75

76
00:05:46,900 --> 00:05:53,050
Now, if you're somebody who likes to write a lot and you want to write multi-line comments, then you can
76

77
00:05:53,050 --> 00:05:54,170
do it like this.
77

78
00:05:54,190 --> 00:05:59,560
You have a forward slash and then an asterisk, and then you can just start typing as much as you want
78

79
00:05:59,740 --> 00:06:01,010
over multiple lines.
79

80
00:06:01,150 --> 00:06:06,670
Once you're done, you type another asterisk and then close it off with a forward slash and everything
80

81
00:06:06,760 --> 00:06:11,870
in between these two areas will be interpreted as a comment.
81

82
00:06:11,890 --> 00:06:14,830
Go ahead and try this out in your Swift playground.
82

83
00:06:14,830 --> 00:06:22,590
See how I can type, and as long as I'm in between these two asterisks, then I can write a comment.
83

84
00:06:22,600 --> 00:06:27,040
But as soon as I'm outside, then this is going to be interpreted as code.
84

85
00:06:27,190 --> 00:06:33,820
And if it doesn't make any sense, then it's going to give me an error. So this is a good segue way onto
85

86
00:06:33,910 --> 00:06:40,230
another topic that we saw: the print statement. As you saw, a print statement is pretty simple.
86

87
00:06:40,270 --> 00:06:46,630
We write the command print and then in between some round braces or some parentheses, we add whatever
87

88
00:06:46,630 --> 00:06:48,640
it is that we want to print.
88

89
00:06:48,740 --> 00:06:55,690
Now, so far, we've pretty much just been printing these strings, but we can also print other things.
89

90
00:06:55,780 --> 00:07:01,900
Let's say if we wanted to perform a calculation in the middle of our print statement. So let's say, I wanted
90

91
00:07:01,900 --> 00:07:09,190
to calculate 2+3. So that instead of printing "Hello 2+3 World" I wanted to print
91

92
00:07:09,220 --> 00:07:14,130
"Hello 5 World" For this little bit of calculation to happen,
92

93
00:07:14,170 --> 00:07:17,930
it can't be interpreted as just text, right?
93

94
00:07:17,950 --> 00:07:19,580
It has to be code.
94

95
00:07:19,630 --> 00:07:23,800
How can we insert bits of code in between bits of text?
95

96
00:07:23,800 --> 00:07:31,630
Well, we can add a backslash and then a set of parentheses and everything that goes in between those
96

97
00:07:31,630 --> 00:07:37,720
parentheses will be interpreted as code. And you can see that it gets highlighted even in a different
97

98
00:07:37,720 --> 00:07:45,640
color, because text is always going to be red and code is going to be highlighted usually in a different
98

99
00:07:45,640 --> 00:07:46,500
color.
99

100
00:07:46,510 --> 00:07:50,630
So now when my code runs, it should say "Hello 5 World."
100

101
00:07:50,920 --> 00:07:55,120
That is obviously being calculated and being executed.
101

102
00:07:55,120 --> 00:08:05,590
So this is how we can insert code into our text. Now, text in programming is usually called a string because
102

103
00:08:05,650 --> 00:08:08,950
it's kind of like a string of letters, right?
103

104
00:08:08,950 --> 00:08:14,150
Notice how each of these letters are kind of just strung together like pearls on a necklace.
104

105
00:08:14,470 --> 00:08:17,620
And that's what we call text.
105

106
00:08:17,800 --> 00:08:25,870
So this way of adding code in between strings is called string interpolation.
106

107
00:08:25,930 --> 00:08:33,490
By using that backslash and parentheses, we can insert bits of code in between bits of text which actually
107

108
00:08:33,490 --> 00:08:38,650
comes in really handy when we're trying to figure out what's going on in our code or when we're trying
108

109
00:08:38,650 --> 00:08:40,430
to test our code.
109

110
00:08:40,450 --> 00:08:53,380
So for example, if you wanted to write the result of 5 + 3 = and then you can simply add
110

111
00:08:53,470 --> 00:09:02,370
a backslash, a set of parentheses, and inside here you can actually make Xcode calculate 5 + 3.
111

112
00:09:02,500 --> 00:09:07,950
And now when it runs, it'll say the result of 5 + 3 = 8.
112

113
00:09:07,960 --> 00:09:17,070
So this is how you can insert code into a string. That's all for this first Swift deep dive and we can
113

114
00:09:17,070 --> 00:09:20,280
now head back and continue building out our app.
