Here's an important question when you're coding stuff, should you use null or nil when you initialize
a variable?
Well, if you're new to coding, you don't know what that means.
Now, Lonell means there's literally nothing inside the variable, right?
So you have a bit of memory space or no memory space.
You have an address for the memory.
And when you query it, there is no variable.
There's literally nothing there.
So if I was going to try and do that in Swift, I would use the magic word now, which means there's
nothing in it.
Now, there may be an issue with this because Knill requires a contextual type, i.e., you can't just
put nothing in a variable.
It goes, hey, what's wrong with you?
Why are you trying to do that?
In fact, if we put.
VAR scorelines.
Because most people think that's the solution, Neal is not an integer, zero is, but nil is not OK.
So the question is, how do you store nail in a variable?
Because there are times when you will actually need to do that.
If you're from the C shop world, this is the equivalent to now, if you're from Scotland, I think
it's no, I can't remember anyway.
So how do we get around this?
Well, we get around this by having what's called an optional.
Now, optional are a very elegant way of handling the now or nil problem.
So let me just check my notes and see what I've got prepared for you here.
Yeah, I know what it is.
The way we tell Swift that this is optional is we put a question mark on it.
So it says this is a box that could contain an integer or could contain nothing.
So when we query, it will get either of those results.
Now, the reason that we have options is because in any software, if you try to access and no value
or no value and you try to manipulate that value, expecting, for example, an integer, then the whole
program will crash because it's a no no reference error or an exception error or something similar like
that.
OK, so where do we move from here?
Well, we can also reassign this score to an actual integer, so now we have score equals one.
So the first case we have nothing, the second case, we have something, how do we get the score?
So if I say print score, what happens?
Well, I'm going to guess that there'll be an.
Well, a slight error, this is a warning, not an error expression implicitly coerced from into any.
This is just Swift's way of getting over the error that I'm introducing here.
OK, so it's not a major error, but it is an error nonetheless.
A much better way to do this is to first check if score is not equal to nil.
So it's going to dive into that variable check, if it's nail, if it isn't, then we have access to
the variable and we can then print the score.
Now, let's see if this gives us an error.
We get the same error, but because we've checked it, we can put an exclamation after it.
And that's telling Swift and iOS that there is definitely an integer in here.
I know because I've checked it.
And I want you just to assume that is correct.
Now, if you get this bit wrong, that's when you're going to get a crash.
So make sure you have actually checked it.
OK, what else can we do with auctioneer's?
Well, we can assign if let's see equal score, we can assign a variable to that optional.
OK, so S.E. is equal to score, if there's an integer in it, then we can print Southsea or we can
do whatever we need to with SC and in that case, we don't have to put the exclamation mark.
Okay, and I like the syntax because it makes it a bit more clear compared to this syntax, right?
Because this one, this is called force unwrapping.
We're unwrapping the variable forcefully.
We're not giving it a chance to gracefully decline and say there's nothing in here.
This, to me, feels a lot more safe than the previous version.
OK, so we've got now a little bit of homework for you.
Write your own optional integer of your age and then print it out if it exists.
So I've given you a couple of templates there.
Go ahead and try that out.