在XCode模板中的ivars前缀有一个或两个下划线 [英] ivars in XCode templates prefixed with one or two underscores

查看:189
本文介绍了在XCode模板中的ivars前缀有一个或两个下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么在XCode模板中(至少在XCode 4.3.2中)前缀一些带有两个下划线和一些只有一个的下划线?

Does anybody know why there are in XCode templates (at least in XCode 4.3.2) prefixed some ivars with two underscore and some with only one?

Master-Detail应用程序模板包含文件MasterViewController.m,其中可以找到:

For example the Master-Detail application template contains file MasterViewController.m, where one can find:

@synthesize detailViewController = _detailViewController;
@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext = __managedObjectContext;

谢谢。

推荐答案

这是一个小虫子。

在@synthesize中使用下划线,主要是为了区分实例变量和访问器名称。这很重要,特别是如果你不仅仅是访问访问器中的变量(例如,延迟初始化)。如果他们是相同的名字,你可以很容易键入foo并获取实例变量,当你的意思是self.foo,它调用访问器方法。所以,这就是为什么在这种情况下使用下划线。

Underscores are used in @synthesize, mainly to differentiate the instance variable from the accessor name. This is important, especially if you do more than just access the variable in your accessor (e.g., lazy initialization). If they are the same name, you could easily type "foo" and get the instance variable, when you meant "self.foo" which invokes the accessor method. So, that's why underscores are used in that context.

现在,苹果已经说过,我们不应该在私有方法 。他们使用该约定作为它们的私有方法名,并且您可能无意中重写了其中一个基类方法。这将是坏的,特别是因为你不能在编译时捕获这个。

Now, Apple has said that we should NOT use a leading underscore in private method names. They use that convention for their private method names, and you might unintentionally override one of the base class methods. That would be bad, especially because you would not be able to catch this at compile time.

然而,他们说可以使用下划线的私有实例变量。事实上,作为一个例子,他们说,在合成属性时,使用一个前导下划线为ivars是完全正确的(并建议)。

However, they have said it was OK to use an underscore for private instance variables. In fact, as an example, they say it is perfectly OK (and suggested) to use a leading underscore for ivars in synthesizing properties.

现在,这里的catch。它们还使用单个前导下划线作为私有实例变量。所以,如果你有相同的名字,你可能会有冲突。为了看到这一点,在实现一个视图控制器方法,他们'_',看看什么代码完成抛出你的方式。

Now, here's the catch. They also use a single leading underscore for private instance variables. So, if you have the same name you may have a conflict. To see this, in the implementation of a view controller method, they '_' and see what code completion throws your way.

所以,他们为什么不鼓励这个命名实践?因为冲突在编译时被捕获。您不能有两个具有相同名称的实例变量。如果你这样做,你会得到一个错误。

So, why do they not discourage this naming practice? Because the conflict is caught at compile time. You can't have two instance variables with the same name. If you try to do so, you will get an error.

现在,他们的代码模板为什么生成双下划线的名称?我不知道。 实现实际上保留前导双下划线(后跟大写字母),但我们看到像__block的ObjC添加。

Now, why do their code templates generate names with a double underscore? I don't know. The "implementation" actually reserves leading double underscore (followed by uppercase letter) but we see ObjC additions like __block.

对于我的钱,我避免任何带有前导双下划线。苹果可以在他们生成的代码中做任何他们想要的。 FWIW,他们为ARC生成的一些代码仍然引用了旧的非ARC关键字,所以我不会把他们生成的代码作为福音。 FWIW,我绝对会保留他们为Core Data生成的代码。在最低限度,你应该改变(在managedObjectContext访问器方法中):

For my money, I avoid anything with a leading double underscore. Apple can do whatever they want in their generated code. FWIW, some of their generated code for ARC still references the old non-ARC keywords, so I would not take their generated code as gospel. FWIW, I most certainly would not keep their generated code for Core Data. At the bare minimum, you should change (in managedObjectContext accessor method):

__managedObjectContext = [[NSManagedObjectContext alloc] init];

__managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

另外,我看到人们使用美元符号作为前导或尾随字符作为他们的私人

As an aside, I have seen people use a dollar sign as a leading or trailing character as their private synthesized name.

@synthesize foo = $foo;
@synthesize bar = bar$;

我一直在写C / C ++超过25年,但我对ObjC相当新,它有自己的复杂。所以,我不是一个语言律师。我甚至不是一个律师助理。所有我知道的是,它似乎是合法的 - 它适用于我尝试的一切,虽然我不能肯定地说,美元符号不应该用于别的东西。 Xcode语法荧光笔不喜欢它(它不会与其余的变量名称颜色)。

I've been writing C/C++ for over 25 years, but I am fairly new to ObjC, and it has its own intricacies. So, I'm not a language lawyer. I'm not even a paralegal. All I know is that it appears to be legal - it works on everything I've tried, though I can't say for certain that the dollar-sign isn't supposed to be used for something else. The Xcode syntax highlighter does not like it (it does not color it with the rest of the variable name).

我希望给你足够回答你的问题,虽然它可能只是刺激你对更多的问题...

I hope that gave you enough to answer your question, though it probably just spurred you on toward more questions...

这篇关于在XCode模板中的ivars前缀有一个或两个下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆