all right i'm live here with 52 weeks of swift back at it with swift properties
today i'm going to just go through and learn more about swift properties and i'm going to
start with sharing my screen let's go ahead and get that started and give me a second for that
okay just had to restart something real quick and i'm going to share the screen okay so back here
swift properties so properties associate values with a particular class structure or enumeration
the stored properties store constant and variable values as part of an instance
where the computed properties calculate rather than store a value
and the computed properties are provided by classes structures and enumerations and so
properties can be associated with the type itself as well so to start with there's stored properties
in simple form a stored property is a constant right or a variable that's stored as part of an
instance of a particular class so that's useful to know and they can be referred to as a variable
stored property and you can also have constants for properties so you can provide a default value
for a stored property as part of its definition all right so let's go ahead and try this out
and we're going to go ahead and paste this in here and what do we got so we have a struct
which is fixed length range and it's going to have a var first value and a let length and so
then if we go ahead and use a range of three items a mutable variable we can actually go through here
and say fixed length range first value zero length three and then say range of three three items dot
first value is equal to 6. all right let's go ahead and run that and it looks like it it works
perfect and so if i go down here and i say stored properties of a constant structure
this will let you create an instance of a structure and assign the instance to a constant
you can't modify the instance with properties even though they're declared as a variable all
right let's go ahead and try that out and in this case we we see that there's a let here
range of four items and then in this particular scenario this will report an error even
though the first valuable is a variable property right and so in this case because
even though this is a var here this isn't going to work because the range of four items was declared
via a let and so it isn't possible to change it even though the first value is a variable this
behavior is due to the structure being a value type the same isn't true for classes which are
reference types okay so that's useful now all right lazy stored properties next year a lazy
store property is a property whose initial value isn't calculated until the first time
it's used so you indicate a lazy sword property by writing a lazy modifier so yeah i do like lazy
evaluation in general uh because it's a great way to build complex things that are performant and so
in particular they say laser properties are useful when the initial value is dependent on
outside factors so this is actually something that i've done with meta programming in python before
is i would have a meta class that would maybe talk to some external resource like a schema file
or something like that and then the entire class structure itself wouldn't even be defined until i
knew what was happening behind the scenes with the schema and this was kind of a cool little trick
that i would do in python because i could uh be adaptive to an api that changed behind the scenes
so in this case this is an example of a lazy store property to avoid unnecessary
initialization of a complex class and so here we go let's go ahead and throw all this stuff in here
and let's paste this in so we have a class data importer as a class to
import data from an external file so again yeah this is something i've done before
in other languages and so in this particular class here we say okay look this this is
going to have all the data and this would provide some of the functionality so that
you would then go ahead and figure out what was in that file and then go ahead and change
the way things are structured based on what is in that actual file so the data manager class has
stored property called data which is initialized with a new empty array of string values so it's
pretty cool to know that you can actually do that kind of meta meta programming or lazy evaluation
so stored properties and instance variables if you have experience with objective c
which i unfortunately do you may know that it provides two ways to store values and references
swift unifies these into a single property so here we go computer properties in addition to stored
properties you can define computed properties which don't actually store value but instead
they provide a getter and an optional setter to retrieve and set other properties and values
indirectly and so python does have this as well uh there's a properties uh decorator
and in this particular scenario we have a struct of points struct size struct rect and then this
is the getter this is the setter and inside of this we go through and we create a square
and we're able to use let initial square center so there's a lot of code here and what does it do
it allows us so it runs so in this particular example it says the point encapsulates the x and y
the size is the width and height and the rec defines a rectangle and so in this particular
scenario you could actually uh the center property is then set to a new value of 1515
so basically that's that's a lot of code for uh building up
getters and setters but if your computer properties set or doesn't define a name
a default value of new is used so this is a little less code so here's an alternative version
that takes advantage of this shorthand and so i think short less code is better
for me at least alternative rect var origin var size var center point and
in this scenario though it is not finding those points which i think are kept yeah so these
would need to be defined the point and size so i need to put these at the top i believe
yeah here we go so it's still using the struct's point and size
but in this case it's a little less code in order to do the computed property there we go
and
so shorthand getter there we go as well here's a shorthand getter we can go ahead and put this in
there we go compact rectangle that works and read only computed properties so i do do this
as well in python uh which sometimes is kind of neat to basically have something
that appears to be a variable or you know some kind of attribute on an object but you compute it
but it's read-only so someone can't change it and so this is a really good pattern i think for many
scenarios where you basically want to calculate a value but don't let someone change that value
and so in this case we say struct cuboid let four by four by five by two and then
we go through here and print this out uh in this case the volume uh of four by five by two is 40
and this new structure is called a cuboid which represents a 3d rectangular
box you can also do observers and so these would would change these are basically a
you know figure out what's happening and so you can say a property observer is for stored
properties you define for ones you inherit and also for computer properties you inherit
and so you have the option to define either of these will set is called just before the
value is stored or did set as called immediately after the new value is stored so yeah this is
definitely pretty fancy programming but basically sometimes you know you may want to do some step
before you actually initialize a value and so this in particular would show you how to do this
and so you would say before i set something do this afterwards do this
and here's our our code so it says will set right here print about to set did set you know maybe do
some kind of calculation so that is that is a nice neat little feature of objective c here
property wrappers allows a layer of separation between the code that manages how a property
is stored and the code defines a property for example if you have properties that provide thread
safety checks or store their underlying data in a database you have to write that code on every
property so when you use a property wrapper you write the management code once when you define
the wrapper and then you reuse the management code by applying it to multiple properties
and so in this particular example we have a decorator here so define a property wrapper
you make a structure enumeration or class that wraps a value and then it ensures that
this always contains a number less than or equal to two so that's definitely pretty interesting
go through here we we run this thing here's our wrapper and if you apply this to a property by
writing a rapper's name then you can actually do this so we can go through here and we can say
struct small rectangle and then you use this wrapper which again in python they call it a
decorator you would say 12 or less var height 12 or less var width and so this basically says uh
insurance dimension are always 12 or less i see so in this case even though you're setting it to 24
it's printing 12. so that is pretty slick actually i like that uh that's a neat little trick here
with uh objective c i'm sorry with uh swift and you can also use it in this particular
scenario as well so i guess the the takeaway here is that this is a a nice way to do
meta programming right you're you're basically really getting into the weeds here and defining
the way things are actually executed and injecting logic that can intercept bad values so you can
also set initial values for a wrapped property so here we go here's a a wrapped property here
which this could be really handy for web applications right where you put default
values in and they don't have to be automatically completed so you can get a good or a mobile app
right this is where this will really come in handy maybe it's auto filled so that the user has a
better experience and then we go through here you apply that wrapper and and you could essentially
ensure that there's a good story for a piece of code you're executing by using these wrappers
let's see what else here so there's a lot more on wrappers here i think i'll play around with this
later you can project a value from a wrapped property as well which looks pretty neat
uh and maybe to wrap this thing up there's also global and local variables and maybe we just play
around something a little easier now so you can use a property wrapper to a local stored variable
but not a global variable or a computed variable so in this case we say func small function and
we use this wrapper and you can also say instance properties are properties that belong to an
instance of a particular type every time you create a new instance of that type it has its
own property values so that's pretty interesting in this case you can define properties with static
keyword and so in this particular scenario the idea would be that you could use a static variable
which i guess we can play around with real quick
and go ahead and run this it looks pretty interesting
querying and setting type properties so type properties are queried and set with dot syntax
like instance properties however type properties are queried and set on the type
not on an instance of that type for example prints some structures dot store type property and
here we go we can go ahead and play around with this thing
it looks looks interesting according to setting type properties
and the audio channels described above are represented by the instance of the audio channel
structure so yeah definitely the the properties here appear to be an interesting uh way to
do some more magical things with your code especially i think in terms of a ux programmer
where this would be where you would use this kind of code uh quite a bit is uh you know essentially
autofilling values or sharing values a certain way and definitely something i'll play around
with more as i as i get deeper into swift so thank you again for 52 weeks of swift
i believe next up next week will be methods which are pretty fun methods or functions
that are associated with a particular type and we'll cover that next week alright see you then