Stephen DeStefano Swift 5 Course - Part 6 (Lectures 23 to 25)
Yes cracking on! Got a bit of time this morning before heading to my Uncle's and Auntie's. I want to cover Optionals then Control Flow. All very doable! Let's go!
Start Time - 08:19
Optionals
So I know about these but good to go over! They're used in situations where they may not be a value stored. Nil means the absence of a valid object.
You can do these for any type at all.
Optional types are returned where there may not have been a value e.g. when converting from string number to actual int number.
The question mark syntax. Good way of understanding this is that the ? QUESTIONS whether something may not have a value. So the question bit is key there!
You can only use nil with optionals. Optionals can only be var as the values are likely to change. If you create an optional without an initial value then it automatically sets this to nil.
Forced unwrapping
Comparing the optional against nil....

That's not forced unwrapping - that's just checking to see if there is a value.

Now the one above does. It uses the exclamation mark after the value. It means that the value is forced unwrapped.
Optional binding - this means storing the optional into another value to see if it is empty or not. You are binding the optional to a constant.

No forced unwrapping used basically. So binding values to temporary constants.
Implicitly unwrapped optional -

And examples of when using these -

10 out of 10 on quiz - woohoo!
Control Flow
I know this well so will be doing some skimming through - hopefully!
For-in
Ok iterating through collections - arrays then dictionaries.

For the dictionary, you need a tuple for accessing the keys and values.
The rest of for-in loops is fine!
While Loops
While and repeat-while are the two variations.

One of the things here is to ensure the counter variable (i += 1) is inside the loop. Not afterwards - otherwise that would run forever!
For repeat-while, the condition to stop the code is at the end of the loop.

So with the repeat-while, you are always going to have the loop executed at least once - it's because the while part (the rule) goes afterwards.
10 out of 10 on quiz. Yeah!
Finish Time - 08:49 (30 minutes total)
I'm going to stop there - little and often for this is best and I have a time schedule to keep to. I'll do more tomorrow evening so I keep the trickle of code continuing. Without using a monitor screen, I'm conscious of getting headaches easily with this. So a good amount for today!
Start Time - 08:19
Optionals
So I know about these but good to go over! They're used in situations where they may not be a value stored. Nil means the absence of a valid object.
You can do these for any type at all.
Optional types are returned where there may not have been a value e.g. when converting from string number to actual int number.
The question mark syntax. Good way of understanding this is that the ? QUESTIONS whether something may not have a value. So the question bit is key there!
You can only use nil with optionals. Optionals can only be var as the values are likely to change. If you create an optional without an initial value then it automatically sets this to nil.
Forced unwrapping
Comparing the optional against nil....

That's not forced unwrapping - that's just checking to see if there is a value.

Now the one above does. It uses the exclamation mark after the value. It means that the value is forced unwrapped.
Optional binding - this means storing the optional into another value to see if it is empty or not. You are binding the optional to a constant.

No forced unwrapping used basically. So binding values to temporary constants.
Implicitly unwrapped optional -

And examples of when using these -

10 out of 10 on quiz - woohoo!
Control Flow
I know this well so will be doing some skimming through - hopefully!
For-in
Ok iterating through collections - arrays then dictionaries.

For the dictionary, you need a tuple for accessing the keys and values.
The rest of for-in loops is fine!
While Loops
While and repeat-while are the two variations.

One of the things here is to ensure the counter variable (i += 1) is inside the loop. Not afterwards - otherwise that would run forever!
For repeat-while, the condition to stop the code is at the end of the loop.

So with the repeat-while, you are always going to have the loop executed at least once - it's because the while part (the rule) goes afterwards.
10 out of 10 on quiz. Yeah!
Finish Time - 08:49 (30 minutes total)
I'm going to stop there - little and often for this is best and I have a time schedule to keep to. I'll do more tomorrow evening so I keep the trickle of code continuing. Without using a monitor screen, I'm conscious of getting headaches easily with this. So a good amount for today!
Comments
Post a Comment