本文介绍了目标C的命名约定/ C,先从" _"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

东西我看到脂肪酶这样定义变量:

Something I see ppl define the variable like this:

b2World *_world;
b2Body *_body;
CCSprite *_ball;

而不是

b2World *world;
b2Body *body;
CCSprite *ball;

我熟悉的第二个,但不是第一个。所以,我查了维基百科有关命名约定:

I familiar with the second one, but not the first one. So, I checked the Wikipedia about naming convention:

名称以双下划线
  或下划线和一个大写字母
  被保留用于执行
  (编译器,标准库)和
  不应该被使用(例如__reserved或
  _Reserved)。

那么,是什么特别的含义是开始_?

So, is that any special meaning which is start with "_"?

我看到它使用_开始的code是在这里:

The code I saw which using "_" to begin is here:

推荐答案

有一些Objective-C的开发商preFIX实例变量以下划线之间长期存在的约定。它可以在几个方面是有帮助的:一,它可以更容易被发现在.m文件的实例变量;二,它解除不得不想出有创意的名字方法的参数,以避免与实例变量的名称发生冲突的开发;三,正如其他人所指出的,这表明该实例变量是私有的,因此不应该在整个code威利愿意不愿意访问。

There's a long-standing convention among some Objective-C developers to prefix instance variables with an underscore. It can be helpful in several ways: one, it makes it easier to spot instance variables in a .m file; two, it relieves developers of having to come up with creative names for method parameters to avoid colliding with instance variable names; and three, as others have noted, it indicates that the instance variables are private, and therefore shouldn't be accessed willy nilly throughout the code.

其实,我要说直接避免超过存取(getter和setter), -dealloc 其他方法访问实例变量-init ... 。这并不是说你应该永远不会使用它们其他地方,但你应该直接在其他方法使用一个实例变量之前,至少可以给它一些思考。

In fact, I'd argue for avoiding accessing instance variables directly in methods other than accessors (getters and setters), -dealloc, and -init.... Not that you should never, ever use them anywhere else, but you should at least give it some thought before using an instance variable directly in other methods.

这篇关于目标C的命名约定/ C,先从" _"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:55