Search code examples
iphoneobjective-ccocoa-touchxcode3to4

Xcode4 templates now use underscore on iVars?


I have noticed that with Xcode4 Apple has updated the application templates to include underscores before instance variables.

// Xcode4
@property (nonatomic, retain) IBOutlet UIWindow *window;
@synthesize window = _window;

.

// Xcode3
@property (nonatomic, retain) IBOutlet UIWindow *window;
@synthesize window;

I know there are differing opinions on the usefulness of this but I was just curious if the updated templates where:

  • (1) Highlighting a new best practice.
  • (2) Showing how Apple does things but meaning for you to do it the old way.
  • (3) Its just personal taste, it does not matter.

Solution

  • It's interesting because in the past (pre-iOS), Apple used to discourage the use of underscore prefixes for ivars:

    Avoid the use of the underscore character as a prefix meaning private, especially in methods. Apple reserves the use of this convention. Use by third parties could result in name-space collisions; they might unwittingly override an existing private method with one of their own, with disastrous consequences. See “Private Methods” for suggestions on conventions to follow for private API.

    But with a modern Objective-C runtime, I believe ivar naming conflicts in subclasses has been eliminated, so this is not a problem anymore. So I think that's why they're making the templates use an underscore prefix by default, to match what Apple's internal code looks like.