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