It's really really helpful, but most people don't know why, and that's a shame. Apple uses underscores to separate the way other objects access a particular object's variables, and the way a particular object access its own variables. Now this may sound a little bit strange, but imagine the following: You probably all recognize the following compiler warning
.h @property (nonatomic, retain, readonly) UITableView *tableView; .m - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self loadSomethingElseForTableView:tableView]; }
This will result in a compiler warning, because it does not know wether you reference to the local variable "tableView", or the instance variable. Therefore, Apple recommends you to add the following to the top of your @implementation.
@synthesize tableView = _tableView;
Now, when you reference to _tableView, the compiler knows that you mean the instance variable, and not the local one.
Also, this makes it a lot easier to understand the Garbage Collection in Obj-C, and to prevent making common mistakes.
For example, when doing the following:
@property (nonatomic, retain, readonly) NSString *title; - (id)initWithTitle:(NSString *)title { if ((self = [super init])) { self.title = title; // Is not possible, since it's read only. title = title; // Is not possible, since it's the same (local) variable. // Changing the method to initWithTitle:(NSString *)aTitle; title = aTitle; } return self; }
Now, since you do not use the default setter (actually, you can't, because it's read-only) you need to retain the variable yourself. This is a lot easier to remember when you give every instance variable a prefix (so you know you need to retain it yourself).
So, basically, it's important to understand the difference between self.variable and (_)variable. (that is: self.variable maps to [self setVariable:...] and variable maps directly to your pointer.
Furthermore, when you add it as a private variable, like this:
@interface TSSomeObject : NSObject { @private NSString *_privateTitle; } @end
The underscore prefix isn't really necessary, unless you may encounter local variables that have the same name. Besides that, again, it's also an easy way to remind you that it's a local pointer and that you need to retain (and release) the variable when you assign it to your object.
What is wrong is to create a property with a underscore prefix, like this:
@property (nonatomic, retain) NSString *_title;
That's really wrong, and I'm not even gonna explain why ;)
So yes! You should really use underscore prefixes, it makes your code a lot easier to read, and to interpret by the compiler! In Xcode 4, Apple even added these @synthesizes to the default templates.