Tip:
Highlight text to annotate it
X
the main pont's of most classes are your instance variables your data and your
methods that icon that data
i have a very simple classier cold
my class it has to instance variables online seven eight score and private
number they're both ends
and one method called along a message
which is defined on the right-hand side and my implementation
and if i jump over into main dot amam
in the main function
i am doing some very simple code here
line eight is instantiated an object of that class
line ten
is sending a message to take all the log message method and one twelve is
releasing it
nothing particularly remarkable going on here were actually outputting a message
that says the score is zero someone because nothing is actually changing
that integer variable called score but here's the question what if after on
instantiated that object
i wanted to change the score
although is no method of that i can access no matter how many things that i
looked up here the would let me do that because while methods are public an
objective c instance variables are private by default and that's the way
you should keep them you know i could actually
change in this line here
to anti-nuke public
which would allow me a rather convoluted way to access them it's not recommended
the recommended way and this is quite common in a lot of object oriented
languages
as you write cortical access or methods liz two ways of doing the subject of c
i'll show you the monthly tedious one first and then show you the
easy way to the easy way only makes sense when you've done the motley
tedious one
so i'm gonna create two method definitions one's going to return an
integer
and all the colts court the same name as the instance variable
the other one i will return void and it will be called set score
and take one parameter
which is an anti
what i'm defining here
is what and other languages are often referred to as your debtors and centers
in fact and other languages
it would be really common to call these methods
gets court and set score
but in objective c it's been a standard for many years to have the debtor
have the same name as the instance variable
well i made that promise in the interface so i better
follow-through within the implementation cell type of dash and then i'll head s
has the quickest way to bring up the score
and that's going to just save return score perfectly allowable because i can
get
to that variable from within my code
in my class make some type of dash and hat
set score
brings across the definition
and then i can simply say scores is equal to whatever was passed in here and
i'll have my access or methods for good both getting and setting the value so
over here in maine what i could doom
is called demo
subtyping nana realizes yes i do have a sets for math of the takes an integer
and let's pass n
i found
and what i could also do of course
is do something simple like spread and anis log message
that uses the score method
to just get to that value
so that wasn't drastically painful but it is kind of tedious particularly when
you end up having a lot of instance variables
and you have to create these gestures and the centers for all of them
but in objective c two point no released a few years ago we have a smoother way
of doing it when i'm going to do
is just delete for those two methods from my implementation and delete them
from
might interface
i'm going to do is replaced them within you keyword
but i'd be outside and then p legal property
and probably is going to allow me
to the generate the declaration of both of the
scores and sent score methods all i have to do is say that it is an emt
and it's called scoring which by convention we use the same name
as the high bar
that's all i have to do in the interface because my implementation
does have to fulfill the promises of my interface so i have to add one line over
here too
it's another at sign
but this one that uses
synthesize
and a photo missus synthesize a property more want property while we have one
called
score
finish the line save
and had come and be to build
the build has succeeded
adding these two lines has
generated
both of these dancer and center access or methods which by convention are using
the standard objective c way of knowing things which means back in my main dot
ham
i have both lyna eleven using the sent score method and line twelve using the
score method to get that value and those just continue to work
so that is how we use properties to generate are accession methods
now one last note here
but young banking two
is that along with the answering property word objective c two point oh
added something called danzig tax it's only christian to size properties though
what it means
they say he online twelve instead of saying square brackets the demo space
score high can actually save demo dot score
now this doesn't work for all methods it's only four
these synthesize priorities
and i can use it not only to get that value but here
where were saying demo sent score and end-user to set that
demagogue ants nonsense gore he it's just score as if there was a directly
accessible property
equals a let's call it ten thousand here say that
and as you can see we are obviously reaching in setting then value than
reaching him
and getting them out here
now while i find dont syntax is a nice little addition in the rest of the
course i'm going to be going back to using the classic objective c square
brackets and then i don't
have to really think about whether or not i can use dot syntax in a particular
place
because it does only work in limited places here
it's completely up to you of course there is no impact on whether or not you
use it
and while a little later on what we're going to see is
how-to and a few keywords to the and sign property declaration to do even
more interesting things
that's a good way to get started generating those access or methods