开发者

Creating connections in Xcode 4 prefixes names with _

When creating a new outlet in Xcode 4 it enters the necessary code like usual, but it prefixes a _ in the header file interface (but not in the properties):

UINavigationController *_mainNavController;
UIViewController *_rootView;

it also does this in the implementation file:

@synthesize mainNavController = _mainNavCo开发者_StackOverflow社区ntroller;
@synthesize rootView = _rootView;

While I can of course use them with the prefixed _, it just makes my code messy. Am I doing something horribly wrong?

Many thanks in advance.


I can of course use them with the prefixed _.

Why don't you use your properties (like self.mainNavController) instead of the backing ivars directly?

Read what Apple has to say about using accessor methods:

Sometimes it might seem tedious or pedantic, but if you use accessor methods consistently the chances of having problems with memory management decrease considerably. If you are using retain and release on instance variables throughout your code, you are almost certainly doing the wrong thing.

Properties encapsulate memory management code and thus reduce boilerplate.

The only places you shouldn’t use accessor methods to set an instance variable are in init methods and dealloc.

Also, the convention of prefixing backing ivars with _ prevents you from accessing the backing ivars directly by mistake (and fail to retain an object, for example).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜