The difference between _ and "I." in Objective-C

Is there a difference between using underscores and using the self keyword in Objective-C when calling @property ?

Property Declaration:

 @property (weak, nonatomic) NSString *myString; 

Calling @synthesize properties:

 @synthesize myString = _myString; 

Is there any difference if I want to use it in my code? When? In the receiver / setter?

 self.myString = @"test"; _myString = @"test"; 
+42
ios objective-c getter-setter synthesize
Apr 26 2018-12-12T00:
source share
3 answers

self.myString = @"test"; exactly equivalent to writing [self setMyString:@"test"]; . Both of them call the method.

You could write this method yourself. It might look something like this:

 - (void)setMyString:(NSString*)newString { _myString = newString; } 

Since you used @synthesize , you do not need to actually write this method, you can just let the compiler write it for you.

So, looking at this method, it looks like it is calling the same thing as just assigning a value to an instance variable, right? Well, it's not that simple.

First, you can write your own setter method. If you do this, your method will be called, and it will be able to do all kinds of additional things, as well as set a variable. In this case, using self.myString = will call your method, but _myString = will not be executed, and therefore various functions will be used.

Secondly, if you ever use Key Value Observing, the compiler does some very smart tricks. Behind the scenes, he subclasses your class and redefines your setter method (regardless of whether it was written by you or you generated by the synthesis) to make calls to willChangeValueForKey: that are needed to verify the key value to work. You do not need to know how this works (although it is quite interesting if you want to read reading in a dream!), But you need to know that if you want Key Value Observing to work automatically, you need to use setter methods.

Third, calling the setter method, even if you rely on synthesis for writing, gives you flexibility in the future. You might want to do something extra when the value is changed, and the moment you find that you want to do this, you can manually write the setter method - if you used to always use self.myString = , then you won you need to change the rest of the code to start calling a new method!

Fourth, the same applies to subclasses. If someone else had to subclass your code, if you use setters, then they can override them to configure functionality.

Each time you directly access an instance variable, you obviously do not provide the ability to add additional functions at this point. Since you or someone else wants to use this functionality in the future, he pays for the use of installers all the time, if there is no good reason not to do so.

+64
Apr 26 2018-12-12T00:
source share

You are right: the first version ( self.myString ) calls the synthesized getter / setter, and the second version directly passes the private member variable.

It looks like you are using ARC, so in this case it does not make that much difference. However, if you are not using ARC, this can make a difference, since assigning a private member does not directly lead to the automatic save / release or copy / release logic that is generated for you using synthesize .

+8
Apr 26 '12 at 12:37
source share

_ (underscore) is just an agreement as described in this question .

If you do not prefix access to the resource using self. , you access the base variable directly, as in c struct . In general, you should only do this in your init methods and in specialized property accessories. This allows you to use things like computed properties and KVC for the intended purpose.

+2
Apr 26 2018-12-12T00:
source share



All Articles