IOS - beginning of iOS tutorial - underscore before variable?

I'm starting to learn how iOS is evolving in a minute, and I currently have an IOS6 book starting with Apress, which I am working with.

Chapter 2 has a simple tutorial showing two buttons and a shortcut, and when a button is pressed, it appears on the label that it pressed.

I finished the tutorial, but he raised one question that I cannot find the answer to.

The tutorial uses ARC (automatic reference counting), if that matters.

Here is the code

Header file:

#import <UIKit/UIKit.h> @interface MTMViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *statusLabel; - (IBAction)buttonPressed:(UIButton *)sender; @end 

And file m:

 #import "MTMViewController.h" @implementation MTMViewController - (IBAction)buttonPressed:(UIButton *)sender { NSString *title = [sender titleForState:UIControlStateNormal]; NSString *plainText = [NSString stringWithFormat:@"%@ button pressed.", title]; statusLabel.text = plainText; } @end 

The above describes how it appears in the book, however, when I ran the tutorial in Xcode, I could not compile the following line:

 statusLabel.text = plainText; 

And instead, I had to change it to:

 _statusLabel.text = plainText; 

When I did this, the code compiled and worked fine, I tried to find out why this happened, returning to the tutorial to find out if I missed something, but did not see anything.

Can someone explain why the code in the book did not compile and why I had to add an underscore to the front of the variable? Is this right or did I do something wrong?

+7
source share
1 answer

Reason statusLabel.text = plainText; failed due to improper access to properties. To access it through the created getters / setters, you need to add self. to it, since the property (and its setters / getters) belongs to the self instance. So instead would be self.statusLabel.text = plainText;

The reason _statusLabel worked because it is a base variable that contains the property value. You access the generated setters / receivers when accessing the variable in this way. As a rule of thumb, you should use self.propertyName , as this will take into account the keywords that you provided as part of the property definition (a good example is to use the atomic keyword, since the setters and getters created will correctly place @synchronized block the base instance variable) .

Recent versions of Xcode create a variable name with an underscore if you do not manually synthesize your properties (which is OK, what you need to do, before people had to synthesize manually). You can define your own base variable name if you wish using @synthesize statusLabel = m_statusLabel . This means that you can access it using m_statusLabel instead of _statusLabel . You do not need to do this usually unless absolutely necessary; Apple suggests using pre-underscores.

You should use the base variable when in the initialization methods and deletion methods, since the generated generated setters / getters can be incomplete when there is currently code.

+11
source

All Articles