okay so we're live here i'm gonna cover another aspect of swift here which is the initialization
and i'm going to first share my screen really quick let's go ahead and do this
and we will let's see let's do
show my screen
yeah so first we'll start with this uh and i'm going to cover today
let's get into here what i'm i'm gonna go to the language guide
which should be under documentation swift language guide there we go so i'm gonna cover
d initialization last week i covered initialization and i'm going to talk about
how a class instance is deallocated and what the dnit keyword is able to do and i'm going to talk
about how swift automatically deal allocates your instances when they're no longer needed
to free up resources and this is through the swift memory management system which is called
arc so i've actually used arc before for a commercial product when it first came out and
it was actually very helpful because i guess i'm lazy i don't want to manage memory i want
a tool to do it for me it's kind of like automl the same thing i would prefer for a tool to to
solve the problems for me so uh here's what we're going to cover i'm going to dive into
opening up another playground let's go ahead and do that next so i'm gonna do this
i'm going to open up another playground and i'll share that next
all rights
and
let's go to this
there we go and i'm going to say file new playground
here we go blank all right got this thing got this thing working
and i will share it
different screen here we go the screen
and
here we go okay so d initialization not necessarily first on the mind
probably of many people but still an important topic what does it look like
it looks like this when you're going to de-initialize something you would type in dnit and
they're called though automatically before in instance the allocation occurs so that is
helpful to know about but let's take an example that they're going to give us here so if i go
through here and i say okay here's a bank class inside we have a static variable coins in bank
and then we have a static function uh distribute coins number of coins let number of coins defend
and in particular here the static function uh will will basically increment here so if i go through
here and i initialize this uh the bank will keep track of the current number of coins it holds
uh and it will handle the distribution the distribution and collection of coins
and then in order to receive the coins uh we could make another one
so we could go down here and we could do we'll say receive coins
that's in here there we go and then each player instance is initialized with a starting
allowance of a specific number of coins during initialization and the player class defines a win
method which retrieves a certain number of coins for the bank and adds them to the player's purse
so that the player class also implements a d initializer which calls a player instance uh
so basically the idea here would be that this i believe is going to wipe out uh things at the very
end and so if we go through and we we call this we've got both the bank and we've got the player
and then we can actually start running this this code so first step here we would we would create
an instance of the player 1 which is an instance of this player class and then we would say a new
player has joined the game and then we would go through here and print this out so here
we go new players join the game with 100 coins and now if we say there are how many coins left
we would go through here and say there are 9 900 in 9 900 coins left in the bank so basically
every player is requesting 100 coins if they're available and this player instance is stored
in the player variable called player one which is optional and this variable is used because the
players can leave the game at any point and this will optionally let you track whether there's a
current player in the game because the player one is optional it's qualified with an exclamation
point and when the coins in the purse property is accessed to print its default number of coins
it will use the wincoins method so let's go ahead and
take another piece of code here and do a little bit more work and so basically we've got this
here we go and so this is showing that the player now only has the bank only has seven hundred
seven thousand nine hundred coins left because player one has uh won 200 coins and now has
uh 2100 coins left and then if we go through here and we say uh here's the player that has
two thousand two thousand coins the player's purse contains twenty one hundred coins in the
bank has seven one hundred left so we can go ahead and print this and if we go through here
we can see that in fact that is able to work and so now that the players left the game this
is indicated by sending optional player one to nil which means no player instance at that point
the player wants reference to the player instance is broken so no other properties or variables
are still referring to the instance so it's deallocated in order to free up memory before this
happens the d initializer is automatically called so this is a pretty good example here of how
when you need to de-allocate things manually in the case of building some kind of game
you can do that by again calling this dnit method here to reduce
basically everything that was in an instance of of the actual player so it is pretty pretty cool to
be able to go to that low level and control things and i think this is a pretty good example of
why you would want to manually de-initialize things and not just let the the
automatic reference counting take place all right well that's it for today i will cover next week
optional chaining which is for querying and calling properties methods and subscripts
on an optional that may be that may currently be a nil okay see you next week