Stephen DeStefano Swift 5 Course - Part 8 (Lectures 32 to 38)
Yes, two in a day! As mentioned earlier, I want to crack on and get the technical stuff DONE! Just want to get on to the projects. So the plan is to get at least collections done, then hopefully enumerations (maybe error handling too!)
Start Time - 20:08
Arrays
Again, as with other stuff in this course, I know this well so more of a skim through.
A couple of cool things. The default value part is the same item repeated basically.
The adding two arrays together means a bigger list basically.

Can use the isEmpty property. Then use that within an if statement.
Other stuff insert at, append, remove all etc.

So using the range ... to put in several values at once.
Iterating over the array. Use of the if let to see if an item is in the array.
Use of sorted function. Then 'by'; you can use the < or >. Or others probably!
Quiz - 10 out of 10 and one of the easiest so far!
Dictionaries
The syntax and concept of these is absolutely fine. Key-value pairs...unordered...can use different types from key or value but must be consistent. Mutability - var or let. All fine.

So the above is the it let again. The use of the updateValue; the unwrapping in the print statement...looks complicated!
9 out of 10 in quiz - a little harder but all OK. Again, there's no point memorising all of this syntax! Just good to get an idea of the concept.
Sets
Sets seem to be a hangover from Objective C. I don't think I've ever used them in a single project. Anyway, they are different to arrays as they are a set of unique values. No repeats allowed!
To declare the type explicitly, a <String> - use of the greater/less than symbols needed. But type inference is used if the keyword Set is used.
Actually quite repetitive - stuff about remove, append, iterating over - very similar to what was shown with arrays.
There are set operations, which are a useful aspects of Sets.
So below are the different things you can do basically.

Union - when this is called across several sets, a new set is created.
Other key features of sets is that they are unordered!
6 out of 10 - clearly Sets is less strong than the other two! No problem - I've learned some useful bits by doing the quiz.
Enumerations
Here we go! I like these - the idea of creating multiple choice cases. Cases are in camelCase but the Enum name is in UpperCase.
Use of switch statement of a value created which contains an Enum - this is pretty cool.
You have case values and associated values. Value types can be different for each case too.
So in the below we have a simple enum for DogBreed. Then we have an (Int, String) for the custom values. These are ASSOCIATED!

You can also add a value e.g. a string but you need to put the type for the WHOLE enum if that's the case. You cannot mix up the type basically.
The text above this arrow should say 'Raw type'.

Ok that makes sense.
You can use the rawValue property to access the value of each case. You can even use character as the type. The rawValue is also inferred by adding on one each time, if that has been started - like on excel!
It can also infer the rawValue of strings if the whole Enum has been set to String as the type upon definition.
Recursive enums - supposed to be advanced...

Skipping past that as it seems unnecessary and far too technical!
10 out of 10! Wow had to guess a couple there. But seriously, enums I'm pretty happy with.
Last bit for today - error handling!
Error Handling
So this is something I used to find REALLY hard but have got to grips with much better in recent times. The whole point of this is using the error protocol...to test for errors...to have code that builds in error handling!
Well he's made all this pretty tricky. Going to have another look - one more go!
So this is responding to potential errors in the code.
So creating your own custom enum which conforms to the error protocol.

So you can then 'throw' an error. You would use the 'throw' keyword and then choose the enum, then the case (the type of error).
If an error is thrown, then a piece of code has to take care of it. You need to be able to identify in your code, WHERE the error could take place!
Keyword try - used for testing to see if there is an error...not so sure about that!
You can also create your own function, designed to specifically test for errors!
OK so with vending machine example, a custom function is created within the class, specifically for testing for errors! If there is a good chance that an error could occur, that's when you need to build in error handling within the code.
Throwing initialisers - you can build this in within the init phase.

This is a do-catch block of code.
Here is one linked to the vending machine example -

Another way of handling errors!

So that is using an optional as the type...the try with a question mark...not as sure about the syntax for this.
Overall, error handling does make sense. It needs to be built in and then handled carefully!
Responding to and recovering from errors that are propagated!
Finish Time - 21:15 (1 hour 7 minutes total)
So another good session! More importantly, I am now done with all the recap/technical stuff! Next time, it will be the what's new in Swift etc etc. THEN the projects! So that's something to work toward, whilst considering the best way ahead in terms of using Treehouse or whatever the best way to learn is!
Start Time - 20:08
Arrays
Again, as with other stuff in this course, I know this well so more of a skim through.
A couple of cool things. The default value part is the same item repeated basically.
The adding two arrays together means a bigger list basically.

Can use the isEmpty property. Then use that within an if statement.
Other stuff insert at, append, remove all etc.

So using the range ... to put in several values at once.
Iterating over the array. Use of the if let to see if an item is in the array.
Use of sorted function. Then 'by'; you can use the < or >. Or others probably!
Quiz - 10 out of 10 and one of the easiest so far!
Dictionaries
The syntax and concept of these is absolutely fine. Key-value pairs...unordered...can use different types from key or value but must be consistent. Mutability - var or let. All fine.

So the above is the it let again. The use of the updateValue; the unwrapping in the print statement...looks complicated!
9 out of 10 in quiz - a little harder but all OK. Again, there's no point memorising all of this syntax! Just good to get an idea of the concept.
Sets
Sets seem to be a hangover from Objective C. I don't think I've ever used them in a single project. Anyway, they are different to arrays as they are a set of unique values. No repeats allowed!
To declare the type explicitly, a <String> - use of the greater/less than symbols needed. But type inference is used if the keyword Set is used.
Actually quite repetitive - stuff about remove, append, iterating over - very similar to what was shown with arrays.
There are set operations, which are a useful aspects of Sets.
So below are the different things you can do basically.

Union - when this is called across several sets, a new set is created.
Other key features of sets is that they are unordered!
6 out of 10 - clearly Sets is less strong than the other two! No problem - I've learned some useful bits by doing the quiz.
Enumerations
Here we go! I like these - the idea of creating multiple choice cases. Cases are in camelCase but the Enum name is in UpperCase.
Use of switch statement of a value created which contains an Enum - this is pretty cool.
You have case values and associated values. Value types can be different for each case too.
So in the below we have a simple enum for DogBreed. Then we have an (Int, String) for the custom values. These are ASSOCIATED!

You can also add a value e.g. a string but you need to put the type for the WHOLE enum if that's the case. You cannot mix up the type basically.
The text above this arrow should say 'Raw type'.

Ok that makes sense.
You can use the rawValue property to access the value of each case. You can even use character as the type. The rawValue is also inferred by adding on one each time, if that has been started - like on excel!
It can also infer the rawValue of strings if the whole Enum has been set to String as the type upon definition.
Recursive enums - supposed to be advanced...

Skipping past that as it seems unnecessary and far too technical!
10 out of 10! Wow had to guess a couple there. But seriously, enums I'm pretty happy with.
Last bit for today - error handling!
Error Handling
So this is something I used to find REALLY hard but have got to grips with much better in recent times. The whole point of this is using the error protocol...to test for errors...to have code that builds in error handling!
Well he's made all this pretty tricky. Going to have another look - one more go!
So this is responding to potential errors in the code.
So creating your own custom enum which conforms to the error protocol.

So you can then 'throw' an error. You would use the 'throw' keyword and then choose the enum, then the case (the type of error).
If an error is thrown, then a piece of code has to take care of it. You need to be able to identify in your code, WHERE the error could take place!
Keyword try - used for testing to see if there is an error...not so sure about that!
You can also create your own function, designed to specifically test for errors!
OK so with vending machine example, a custom function is created within the class, specifically for testing for errors! If there is a good chance that an error could occur, that's when you need to build in error handling within the code.
Throwing initialisers - you can build this in within the init phase.

This is a do-catch block of code.
Here is one linked to the vending machine example -

Another way of handling errors!

So that is using an optional as the type...the try with a question mark...not as sure about the syntax for this.
Overall, error handling does make sense. It needs to be built in and then handled carefully!
Responding to and recovering from errors that are propagated!
Finish Time - 21:15 (1 hour 7 minutes total)
So another good session! More importantly, I am now done with all the recap/technical stuff! Next time, it will be the what's new in Swift etc etc. THEN the projects! So that's something to work toward, whilst considering the best way ahead in terms of using Treehouse or whatever the best way to learn is!
Comments
Post a Comment