okay so i'm live here with 52 weeks of swift and i'm continuing my journey through the language
and right now i'm getting into the concept of initialization which is the process of
preparing an instance of a class a structure or enumeration for use
and this process involves setting an initial value for each stored property on that instance
and performing any other setup steps that are required before the instance is ready for use
so this would be really common in gui development you would potentially initialize a bunch of state
for some aspect of the ui and this does come in handy because you don't need to
write a bunch of extra code it solves a lot of this initialization process for you
so i'm going to go ahead and get started here and share my screen and i'm going to
start off with sharing this window in the playground and i can probably change the layout
a little bit better here yeah this is a lot better that was too big i want that layout and i think we
can just stay right here this looks good okay so uh next up what i'm gonna do here is i'm going to
start down the road of playing around with some of the initial code here now
to set up an initializer first up what we would do is uh put this init method right uh so init
uh and then we would go through here and do some kind of initialization when i have a new
instance of a particular type now let's go ahead and build this out first by building a structs
and this would be a good example again from the swift programming language guide here is i
build out a struct which is fahrenheit inside of i have a mutable variable which is a var
and i create a double which means i have floating point capability and then i initialize inside of
here the state so this looks pretty clear how would i actually use this if i went through
it and i made an instance of this struct i said fahrenheit maybe here like that that looks good
and then i want to go ahead and print something out let's go ahead and do that
we'll say prints false hemp is and then i would go through here and do some string substitution
so i'll use the fact that this attribute now is basically built on top of the instance which is
pretty cool and i can say the default temp is temperature and then i can put in you know f
here and let's go ahead and print this out what do we see well we see that this should be able to
run here and then run here and we see there we go the default temperature is 32. so really is
handy that you can put this stuff inside you know initialize every instance of the object with some
set of values again it could be like the ui layout you want it to be laid out a certain way it goes
in here and and does that now another thing we can do as well is we can have default property
values so you can set the initial values of a stored property from within an initializer
by assigning an initial value to the property when it's defined and the way we can do that
is basically first doing this so let's go back here and uh put this right here so we say struct
var temperature is equal to 32 and we can also do basically the same thing
using that method so how about customizing initialization uh process uh so you can provide
initialization parameters as part of initializer's definition to define the types and names of values
that customize the initialization process so in this particular example we say the
following example defines a structure called celsius which stores temperatures exposed
in the degrees celsius the celsius structure implements two custom initializers called init
from fahrenheit and a knit from kelvin so let's go ahead and take a look at this
paste this code in and see what we can find out so we see a struct celsius and then inside i have
a mutable variable temperature in celsius i then initialize the from fahrenheit here and so i go
ahead and i do a conversion and then i initialize a kelvin and this is the celsius temperature
is basically kelvin minus 273. so we do some some basic calculations inside of here
and those are included and then how would i actually use this well let's first initialize it
and then let's define a variable so we'll say let boiling point
of water equal to celsius and then we'll call that from fahrenheit and then
let's go ahead and do that from fahrenheit and we'll put in q12.0 okay
and then what does it get so we were able to actually set that and now if i want to
print this out i could just print boiling point of water and there we go we see that
it's 100 degrees and then if i want to do the next one let freezing point uh freezing
point of water next we can do this we can do celsius from kelvin i'll put in 273.15
and then again go through here and just print this out freezing points freezing and
we need to we have a mistake here there we go let freezing points water right here okay
we print this out and we see the freezing point is zero so you can see how this it really helps
actually with mathematical calculations as well so what else can you do you can do parameter names
and argument labels so just like a function and method parameters initialization parameters can
have both a parameter name for use within the body and an argument label for when using it so let's
go ahead and run some additional code here so we have a new example which is a struct that has
color we have let red green and blue so basically make those mutable but i'm sorry immutable but
then go ahead and initialize them uh and and then we also have a knit white which is red is equal
to white green is equal to white blue is equal to white so basically both of the initializers can be
used to create a new color instance by providing named values so if we go through here and we say
this we say let magenta equal red 1.0 green zero blue one and i can actually print magenta
there we go and then if i want to do half gray i could also go through here and do lead half gray
and prints half gray and you can see all of the attributes inside of this right
so it's able to retain that so initialize parameters without argument labels as well
here's another example for us if you don't want to do an argument label we can do this struct celsius
var temperature and celsius and then from kelvin and knit here let's go ahead and run that and we
can also say lead body temperature equals 37 so a lot of neat little tricks here that we can do
by by using these initialization methods body temperature there we go
uh so what about option optional property type so if the custom type has a stored property that's
logical to have no value because maybe you can't set it during initialization
or because it's allowed to have no value you can declare the property with an
optional type which is definitely kind of cool and how would you actually do this the way
that you would do this is properties of optional type are automatically initialized
with a value of nil the following example that we're going to take a look at has a survey
question with an optional string property called response so let's go ahead and take a look at this
if we go through here we can see the class which is a survey question we have immutable variable
text immutable variable response we initialize it and then we we notice here that this is an
optional type string here and then we have a function which actually prints out the text so
let's go ahead and call this we'll say and here's the here's actually the problem we'll go
let cheese question do you like cheese and then we can do cheese question dot ask because this
instance will automatically inherit this function so we can do that we can say cheese question
dot ask don't just say do you like cheese and then if i want to put a response in here because
we don't have anything there yet because if i go cheese question uh
dot response notice that there there is actually nothing because it's an optional type
there's nothing that's nil but we can actually uh specifically set that here so we can go now
set this and we can do this we can say g's question
dot we can say yes cheese is excellence
in omelet in fact i did this today i had an omelet cheese and i can go through here and then do the
same thing let's just be lazy and print this and now we can see i've set the response and there
we go optional yes uh cheese is excellent in my omelet you can also assign constant properties
during initialization as well uh once a property a constant property is assigned it can be modified
which is also kind of kind of nice and you could also have the concept of a
default initializer for any structure or class and so here's a good example of that
is that we were going to define a class called shopping list item which encapsulates the name
quantity and purchase of things in a list so let's go ahead and try this out let's delete this go
ahead and do this so here's a shopping list here and then if i want to make an item in the list i
would just go through here and make an instance of this there we go item shopping list item
and then from here if i wanted to say basically var dot quantity r dot quantity what do we get
we have um basically so we can put this oh sorry item.quantity item to a quantity
we we should see that this should print out um there we go uh you can also do member wise
initialization you can do initializer delegation you can do a lot of very sophisticated
initialization tech techniques here the other thing you can do is you can do class inheritance
and initialization as well which is which is kind of neat and you can also do initializer delegation
as well so you can do very sophisticated methods for for going through and building things out
and i guess we could take a look at you know some of those methods here um but
i would i would say that's probably good enough potentially for what i wanted to play around with
and uh maybe let's see is there anything else here available initializers for enumeration
properties propagation of initializers yes some of this stuff i think is a little fancy for me
and i would potentially not play around with it other than let's maybe close with this
which is setting a default property value with a cons a closure or function i do like closures
so here here's an example of a closure all closure is is like a nested function so we have a class
some class we do a let right sometimes so this is essentially the pseudo code right is that we we
could actually return back in fact the function that's in here right so it returns back a copy
of itself so basically a closures is is a curly brace followed by an empty pair of parentheses
and so let's go ahead and try this out if we were going to build out their example and we say
a struct which is a chess board we let the board colors be a boolean value we do temporary
board bool var is black false and we say we basically do a little logic some nested loops here
and then notice that what we do is we we we return back uh the
the bool value right here uh you know based on what happens here and then we have a function
which is square is black at and this one will return back the board colors right
and so this is this is essentially taking out this uh function and returning it back
uh to us so how would we actually use this well we would say let board is equal to chessboard
right here and then we could actually print out the board as that square right so so basically
uh this return this keeps track of the state and that's one of the values here of this
is that because we're using this closure it can it can understand you know essentially
what happened in the history right in the past here and that's really one of the the values
of a of a closure function all right so this is a good place to stop for today and i will stop
sharing my screen yeah so next week we'll cover d initialization and d initialization is something
that is called immediately before a class instance is deallocated right so if you made an
instance and you wanted to set some state you could also do a d d initialization so right before
the memory is cleaned up with automatic reference counting or arc you could
actually go through and do some other you know clean up actions okay see you all next week