Bob Lee Course Part 8 (lectures 12 to 15)
Next day, next lecture! I literally have 25 minutes now so I will see if I get to a natural end point...
Extensions
Now I know about these already. Extensions are a way of adding more code to different types. E.g. to Int, I could add a function or something that gives more usability. I can also extend custom types e.g. structs and classes.
*Udemy course not loading properly. Going to have to delay start time for now.
Start Time - 15:39
*I only have around 15 minutes for now so will have to continue the entry for later.
OK, so the issue here is that you CANNOT have STORED properties in extensions.
Right, so in this case, the description property is computed.
So this is a cool example of using a new property (computed!) for Int. Squared!
So that is everything we can do with extensions. Most of these are later as you can see.
So far, that's good. I had to race through some of the using init and subscripts for different types so will have to go over that. But all good so far about extensions; most of that felt familiar. The key bit that I had forgotten was that properties need to be computed - not stored!
Paused at 15:48 (9 minutes so far)
I will be back with the next lesson in an hour or so!
Restart at 18:35
Here we go - I'm back for the next part!
The ternary operator - apparently this has replaced if...really?! OK we shall see!
So Bob's coding mentality is all about less is more. I agree to an extent but I don't have the knowledge to put that into practice yet!
Unary Operators
Right, so first of all it needs to be based on an existing type so the example I have used here from Bob is using myEmployees - the type alias - which is being used in the function. I can use either the original (Employee) or the typealias (MyEmployees).
I get it now, just don't really see the ADVANTAGE....
Extensions
Now I know about these already. Extensions are a way of adding more code to different types. E.g. to Int, I could add a function or something that gives more usability. I can also extend custom types e.g. structs and classes.
*Udemy course not loading properly. Going to have to delay start time for now.
Start Time - 15:39
*I only have around 15 minutes for now so will have to continue the entry for later.
OK, so the issue here is that you CANNOT have STORED properties in extensions.
Right, so in this case, the description property is computed.
So this is a cool example of using a new property (computed!) for Int. Squared!
So that is everything we can do with extensions. Most of these are later as you can see.
So far, that's good. I had to race through some of the using init and subscripts for different types so will have to go over that. But all good so far about extensions; most of that felt familiar. The key bit that I had forgotten was that properties need to be computed - not stored!
Paused at 15:48 (9 minutes so far)
I will be back with the next lesson in an hour or so!
Restart at 18:35
Here we go - I'm back for the next part!
The ternary operator - apparently this has replaced if...really?! OK we shall see!
So Bob's coding mentality is all about less is more. I agree to an extent but I don't have the knowledge to put that into practice yet!
Unary Operators
!true
!false
So with these, it is using the ! to show the opposite of something
Binary Operators
1 + 2
4 % 2
7 == 8
These are binary. But why? Let me pause Bob to check....
OK from the Swift Developer handbook...
- Unary operators operate on a single target (such as
-a
). Unary prefix operators appear immediately before their target (such as!b
), and unary postfix operators appear immediately after their target (such asc!
). - Binary operators operate on two targets (such as
2 + 3
) and are infix because they appear in between their two targets. - Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (
a ? b : c
).
Right so the un/bin/ter bit is linked to the number of TARGETS. That makes a little more sense!
Here is an example!
let watchSimpons = true
watchSimpons ? print("Which episode?") : print("No Simpsons!")
Makes total sense and MUCH easier than an if statement!
for number in 1...50 {
number % 2 == 0 ? print("\(number) is a multiple of 2") : print ("\(number) is not a multiple of 2")
}
That's me adapting one of Bob's examples. Really cool!
Definition of an operator -
AN OPERATOR IS A SYMBOL FOR A FUNCTION!
I've KIND of done this on my own -
var driverWins: Int? = 0
driverWins! > 0 ? print("Driver has \(driverWins!) wins!") : print ("There are no wins for this driver")
But I'm not sure about all of the forced unwrapping...
var driverWins: Int? = nil
var minimumWins = 5
var newWinsRecorded = driverWins ?? minimumWins
Right I've got my head around this. This is about the NIL COALESCING OPERATOR. What this means is that a variable (I've created a new one - newWinsRecorded) in the line of the code will store the value of either the var/let on the left of the ?? if it does NOT contain nil (has to be an optional); if it does contain nil then it will use the value of the var/let on the right of the ?? - in this case 'minimumWins'.
Cool! That does make sense.
A nice quote from Bob (used from a random book he has mentioned...) is to always find the why, then you fill find the how!
Type Aliases
Yes I am cracking on with the next lesson! It makes sense to do so as this is the last one for this entire chapter. After that, there is a conclusion, then I will do a consolidation entry next time!
class Employee {
}
typealias MyEmployees = [Employee]
func listEmployees(enterEmployees: MyEmployees) {
}
listEmployees(enterEmployees: [Employee(), Employee()])
No idea what all of this means yet!
From medium website:
A type alias declaration introduces a named alias of an existing type into your program. Type alias declarations are declared using the keyword typealias and have the following form:
typealias name = existing type
After a type alias is declared, the aliased name can be used instead of the existing type everywhere in your program. The existing type can be a named type or a compound type. Type aliases do not create new types; they simply allow a name to refer to an existing type.
Right, so first of all it needs to be based on an existing type so the example I have used here from Bob is using myEmployees - the type alias - which is being used in the function. I can use either the original (Employee) or the typealias (MyEmployees).
I get it now, just don't really see the ADVANTAGE....
typealias GridPoint = (Int, Int)
func enterPoint(grid: GridPoint) {
print(grid.0)
print(grid.1)
}
enterPoint(grid: (1, 2))
Paused at 19:41 (1 hour 6 mins)
Restart at 20:15
Ok so with this example, two different dictionaries have been created - both with the same information.
Right so I don't see the link between type alias and arrays/dicts there.
Bob then does a summary of this first chapter. There lots of useful bits and pieces! Before I start chapter 2, I will be doing a consolidation of the last few, especially as there was a one week gap at one point!
Finish Time - 20:30 (5 minutes more; total time - 1 hour 20 minutes)
So that is chapter 1! Looking over it, I have learned lots of new concepts and refined some others. Here is a VERY general summary - looking at each aspect - optionals with chaining, binding and the use of the guard statement; error handling and why this needs to be built in; type casting, including both up and down casting; generics, subscripts (not too sure on the usefulness of these yet), classes vs struct - in terms of value and reference types; sets and tuples; extensions; operators including unary, binary and ternary; type alias - again, not sure about when/where to use this one yet. So that's it in brief but I like I said I want to go over this again. I would actually like to do the whole lot again, rather than just the last few. That's what I'll be doing next time before beginning chapter 2. Thanks Bob!
Comments
Post a Comment