So or come back in this class and begin to talk about a very important topic of reference counting reference
counting is essentially vaid that you assign your memories and how you decide whether a new value of
your property is going to have its own memory space or whether it is going to point to another memory
as ways and how often they do get are located and how often they get released.
Now the first counting is specifically in Objective-C is not the easiest thing to teach and to learn
but there are some very good practices that simplify this for us in this case and I will go through
almost all of the reference counting strategies that are out there and I will try to come up with some
of the easiest ones for you so you can use them something you want to keep in mind is that you probably
are not going to use any of these for a very very long time for the most part you can actually stick
with the default ways of doing things and nothing is going to go wrong.
But then the time will come that you are doing something a little bit more complicated and you have
objects that they have internal references to each other and then you need to know how to deal with
those.
So with that in mind the squarehead into Estcourt and in code I'm going to start a new app command line
tool Objective-C.
I'm going to call it R R.
And Orkus stands for automatic reference counting that's like the closest thing you can find in our
X code for garbage collection for finding what object is no more in use and has to be released and what
objects they have to stay in their stack because somebody is still accessing them.
Well I'll call this one Aurukun.
Let's say read or read right.
Because that's the first thing that they want to talk about and of course these Objective-C
then the yeah I'm going to go ahead and say I have a class interface let's call it my class.
It has a superclass of this object.
And then of course I need to have my implementation of my class and in my implementation I have nothing.
Because simply just want introduce some properties as property envenomed.
I'm going to make it like this and when I say it is atomic it is also read write and it is also strong.
And I'm going to call this one the full string.
So what does it mean.
This is the default behavior of Objective-C.
It means when you define any variables by default they get to become atomic read right and strong.
So theoretically writing this line and writing this one are exactly the same thing.
It would automatically would have those qualifiers added to it.
I'm going to call this one or fall the string.
So that's the easiest thing.
So we got that out of the way.
Now what is difficult or not difficult but what we need to discuss is so what all these things you know
what do we do with them and how do we use them.
Let's go ahead and get started.
The most basic one which is this one reads right.
I'm going to say I have a property and that property has only one qualifier hard to read right and then
I have the same thing.
Once again this is called My read write a string.
So this is a read write to string and then I have another one which is read only a string.
And I'm sure you can imagine what happens now if I go ahead and make two of these properties in my file.
Anyway I am I'm going to say my class my object is my class knew that I could go ahead and say my object
got read right becomes hello but if I try the same thing we might read only
look what happens.
It gives me an error saying that hey you cannot set anything to a read only property simply because
the only properties are meant to be only changed as an initialization level.
So for that you cannot change anything.
So because of that behavior we can only set the read only by using an EVA.
And that means using the set to read only a string which is something that we often do when we want
to do some internal calculations.
So more of this will come in in a few minutes and we're talking about eyeballs.
But for now let's say that for most everything you probably want to use or read write which is a part
of the default behavior.
So let's keep that in mind and let's move on to another example which we're going to talk about atomicity
of a variable.
So for doing that I'm actually going to go ahead and start a new project let me copy everything from
here because you're going to need a lot of these things.
And when I go in here and say command line tool are you putting more.
Tomas the object is c and then in this one I'm going to paste everything.
So here on the top forget the default ones if you know what they are.
You could add everything in the same project.
For me I'm trying to have everything in one screen.
That's why I like it this way.
So the laugh then becomes atomic.
One of them becomes none atomic.
And if you remember atomic is the default behavior.
And of course I'm going to change names to atomic string and non atomic string.
So let's see what these two things are.
I'm only going to write two keywords for each of these in the hopes that you know that would kind of
explain it for what's coming up.
Atomic is multithreaded and lock non-atomic is single threaded and unlock.
Let's see what all these things mean.
Thomas It essentially is about the way that something gets access.
Imagine if you have two different threads or three different threads are trying to change a value of
something.
Maybe you have a thread that is you or you are reading you are seeing things and then you have another
thread which is trying to kind of cure back and then read some information.
I you have another thread that you are doing some calculations.
So it's very common to have multiple threads at all times.
So in a situation where all these threads are trying to access the same value it becomes a little bit
unclear of which one is accessing it first which is writing it first.
And when we are trying to read it tonight which of the values we get access to now in a situation where
you have a variable which is atomic.
It means every thread can simultaneously access it.
It means every thread before the access that they will lock its value.
So in the meantime while doing something on it if you try to access not one you see the previous value
which is a good thing.
It means you will get some value back your value will not be needed because that trade is doing something
that is the downside of it is you might not get the correct value.
It might be that you're getting the value right before an operation and you're not seeing what is happening
inside the operation.
Now in a non-atomic value Well what's happening is is only one thread that can access the value at any
given time and it doesn't try to lock it.
It just goes on and it starts changing the values of that viable.
And the downside of it is obviously you might get a near lvalue.
So let's see this in practice.
And I should warn you that some of the gold I'm going to write in here is a little bit at last for us.
We're going to see these in in a few sections when we talk about trading sessions.
But for now we kind of need to just just see them as a demonstration.
Don't worry about specific ways that they work.
So here I have my object and I have my object dot atomic string and I'm going write most of them in
one line saying is my non atomic a string.
And they both have a let's say a default value.
So by now we have an object that both of these things are set to something and we know if we print these
are we're going to see essentially the same thing.
So this next thing is going to be quite new for us and that is how to start a new thread.
So solving it two ideas usually are an operation that happens by something else you know when you access
to a network or you might want to manually you know write thread to update your you I but here when
I started that word on my own I'm going to say dispatch in a dispatch from say dispatch a sink dispatch
a sink and the one I want to use is this.
CH This gets main gets global Q And then for the identifier I want to use that dispatch you are priority
bygones.
No flags.
Here is the body of the code.
So this piece of code essentially is going and starting in thread for us and then I start this new thread
I'm going to do this.
I'm going to save both of these values.
Let's give them something else let's call it the second value.
So that is Sanken value.
And then I'm going to go ahead I start another thread in here and say both of them they get it here
at the rally.
So what's happening.
We have a default value that is our clients value.
Two other threads are accessing these values and changing them to other things and they're all happening
simultaneously at the background level.
So the very important question becomes so what is the value in either of these two right now.
So if I go in here and I say a slide and I would say are atomic is.
I would say my object not atomic is string.
And I would copy that and say non atomic is not atomic a string.
We should see what's there is not sought here.
If I run this it shows up.
Now here we see that atomic kids are still defiant.
So atomic is reading the value that was there.
Those other two threads haven't managed to change it.
Non-atomic is the second value.
So if I go ahead and say well let's go to one of these threads and change their priority.
You say high priority.
I run this one now my non atomic value reflects the most recent update.
It is as you know a single threaded.
It's not locked.
It goes and says well finished this thread finished that thread finished that thread and then give me
the value.
One of my atomic s thing says well I'm just trying to get you the result and the result I can find the
right of way is whatever it was there.
I'm not going to worry about those other threats.
So there is some advantages to using either of the two and the situation will come for you when you
know that you have to use non-atomic.
But for most everything you can use atomic which is the default behavior in X code anyway.
So that was about the atrocities of a value bet.
Now let's move on to another topic and that's another off whether an object is being copied or it is
being assigned.
So we'll see what that means in a Cygan like the last time I'm actually going to copy everything from
here and then I start a new project this new project I'm going to call it to argue copy versus assign
Objective-C of course.
And bear with me as I paced this and I change a lot of stuff.
So off the top I don't have to worry about the atomic part anymore.
I'm going to change one of them to copy quali copied the string and then I'm going to change one of
them to sign and I'm going to call this one.
I signed this thing and then it changed that effect.
And in here I don't have any of those threading and whatnot but I do have is that they both have one
initial value let's call it copy the string is assigned a string.
I don't want them boys to be looking at the same static value I wanted it to be actually inside another
variable.
So I'm going to go ahead and say how I in this mutable string and when I call it a value which is an
S mutable a string string by a string and the other string says default.
So the reason I want it to be inside an estimate of a string is because I want to change that value
really soon.
So I'm going to say most of these guys they are value so whether they are copied or assigned for that
is copy or assign they are going to be the same value of a value which is on an estimate about a string
for the next piece is actually very easy once there are those assigned to one I'm going to go ahead
and say Well Van has actually had some stuff at hand and some new things.
So as you can all imagine right now the value inside the value is the default space.
And some new things.
Now what happens if we print or copy this thing and assign the string.
So I'm going to say copy it.
Copy the string I signed and signed is going to be signed the string.
Let's run this project I execute that you should see down there my so that copy is a default.
It doesn't have those extra bits of text and a sign is having everything.
So this is what it means.
It means that copy any value but of the type copy.
When ever you assign them whenever you initialize them they make their own copy of the value and they
place it in a newer spot in the memory while a sign essentially keeps a pointer to that part to whatever
was of value in there just so that you know we know a sign is very similar to the page and copy is very
similar if you are strong and that's something we're going to see in the next example.
What is the difference between these two guys.
So to demonstrate the relationship between a strong and Veeck qualifiers I'm actually going to go ahead
and start a new project and this one I'm actually doing it in suites simply because that's something
that we use more often in swift.
I'm going to say are strong.
We are stryfe in here.
Here's what we're going to do.
I won't write an app that has a very basic user commenting relationship.
So here go ahead and say on the very top I have a class called common.
Then you have another one class.
This one is users.
So we have two classes so far they have common and we have usage so for comment I have about our text
which is going to be empty at the beginning so that's it takes the body off the comment.
I also have a use for it.
I have a that I'm going to call it on there and that's going to be a user.
And that's essentially who has written that comment.
Now for a user itself every user has a name which by default is empathy.
And then every user also has a Carman's
Harmons which is going to be our comments of course.
Now you know in real life scenarios this obviously would be an array of comments.
But for now I'm trying to simplify things and I'm assuming one user can comment on what other user and
vice versa.
Nobody can comment on multiple different people.
So that's that and of course I need to have an initialiser in it here.
Can to say in name.
And I think that's pretty much all I would need because come on they have to just start a new one.
So that's easy I'm going to go ahead and say self-taught name becomes input's name and I'm going to
say self-taught Carmens becomes a new comer.
So so far we have two classes that each of them deals with something one of them deals with having writing
comments.
The other one deals with holding onto all of our users.
Now the tricky thing here is the balls have a variable of each others type within them and that's somehow
a common situation.
You know you're right you're a staff innovator.
They have data relationship between them and we have to find a way to make sure that our retained count
doesn't go up beyond what's meant to be.
So here's how they're going to test this whole thing.
I'm going to go ahead and say let's make one use of let's say my user that's going to be a user of.
And the only thing I have to give it is John.
Right.
So I have a user with the name John.
And then I'm going to go ahead and preens how many retain count I have for this user.
So I'm going to go ahead and save print in my print.
I'm going to go ahead and say our users count is what is it.
Is this something that you know is perhaps new for you and that is get retain count.
So I'm going to say see I get to retain count.
And what I want to get the count of.
I want to get the count of discard.
So what it does it gets how many copies of these are in the memory.
And by default this should be torn out because it should be one for the actual valuable and one for
the hour.
So let's run this
and once or up front it says the user's account is true.
So that's so far perfectly working the way we expected it.
Now what happens when I do this when I say my use of dot comments which is that valuable in here.
Dot on there which is this you in here becomes my user.
So sure I think you could have a lot of situations where a user has commented on themselves or tool
users or to use as they have commented on each other and each of them is being used both as the guy
who owns the comment as well as the guy who writes that comment.
So now my user comment owner has become my user.
And now let's go ahead and reprint this user accounts.
Now is
trick.
And here's the problem.
The problem is that the next time we were trying to point to this user through this comment on our not
only we pointed out that but also he copied it in a newer space in our memory which is really not a
good thing.
We ended up having more than one copy of my user and that's being assigned for only this one.
So to remedy that I'm going to go ahead and say that it's a very simple fix.
This on average which is only going to be a point there has to be a weak link.
If it is a weakling it's means like a sign don't copy it means do not copy the value.
So let's go ahead and say it and now both counts are still too simply because here be are setting that
we are not adding another account
and finally in this lesson we're going to talk about uveitis or directly accessing valuables.
Let's see what they are.
I'm going to start a new project in export objective c and this is going to be.
It has our eyes objective see.
Let's see what they mean.
Imagine we have a class up here I have an interface interface Let's call this a user class and as object
as you know we don't know if you need anything else for it.
I also have implementation and that's going to be a user.
And then I have some functions in here and I'm going to call this one some function doesn't matter what
it does some function.
Now if you remember whenever you had a situation like this and you had a property that property and
that property was friends and as a string to some property we could do two different things when we
wanted to access that property.
You could either say on this or some property that becomes hello and all of it would say self that some
property and that becomes held.
So what's the difference between these two.
I'm actually going to write it V-twin here within the actual body of it.
This here says directly accessing the value of that.
And this one says accessing very well Rudy said.
So what is the difference between these two things.
When you declare a property using the property in Objective-C it automatically does something that we
call synthesize and what synthesize does is essentially sets the two variables sit there and get there.
So what you don't know is by default this thing now has something called Set some property is actually
a function called void set.
Some property is already being built which receives a property and sets that.
And if you use this dot operation you're essentially accessing that method.
If you use the underscore one you access the variable directly.
So in most cases that doesn't really make a difference for you.
You could use either or simply because you don't care about what is within the body of the set.
But if you want it you could manually do the sender.
So go ahead add in your value.
I'm going to call it manual property for my money or property I'm going to write a boast that said there
and they get there myself I'm going to go ahead and say void them.
And here is the set mining all property to something and then I'm going to go ahead and say and that's
a strange because it is of the type of string right manual property return.
What should it return it should return the cell the manual property.
So here is how I have essentially written Boerse of these manuals.
And in this set we should receive an input value that you can call it nine you want to recalculate new
value and then it of course has to have the same self that non-null property has to become new value
or.
And this is the only time that this happens or you might not want to do that or you might want to do
something before you sign for whatever reason you might have a situation that you need to do something
before you sign it.
So that's essentially how these two will be different.
You could get used to using either of them.
I personally for the most part I use this system accessing through the other simply because of this
very fact if I decide to change something in there I can easily go ahead and do it and if I had so many
of these being applied like that I can always just change something in the solder.
And they've all will be affected by it.
Well if I use the IRA if I use the direct taxes then I have to go to each of them and manually change
that value.
So that's about that are AMANDO a quick recap of everything that we did because it's kind of a lot of
stuff in this one lesson.
So here we did these things we talked about atomic on non-atomic.
If you remember this was a single trial that might be nil.
This one was multithread that never Nan might be wrong though it might show you the wrong value.
We talked about a sign on copy and we said in the sign of point a location in memory coffee or Surely
I typed that the wrong points copy essentially copies information for itself.
That's about that which is a weak link.
Existing as long as someone has connections to it are the strong is a strong link living for as long
as the cycle lives.
These warnings aren't exactly right that for instance would be kids essentially saying maintain it as
long as someone else has a strong relationship to it.
What I'm trying to I don't know in my own way I'm trying to simplify things a little bit.
So that's about that different qualifiers for properties.
For the most part we stick to strong.
And if you're so go read the right of your so go for it.
But there might be situations that you decide to change any of these.
Like I said you can live with the default values for now and when the time comes.
Hopefully by then you will know yourself if you have a change of those with that in mind on this very
long lesson finished let's move on to our next lesson.