Xcode4模板现在在iVars上使用下划线? [英] Xcode4 templates now use underscore on iVars?

查看:67
本文介绍了Xcode4模板现在在iVars上使用下划线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到,苹果使用Xcode4更新了应用程序模板,在实例变量之前添加了下划线。

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)突出显示一种新的最佳实践。

  • (2)显示苹果如何做事,但对您而言意味着做旧

  • (3)只是个人品味,没关系。

推荐答案

这很有趣,因为在过去(iOS之前),Apple曾经不建议在下划线中使用下划线前缀

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

避免使用下划线字符作为前缀,表示私有,尤其是在方法中。苹果保留使用此约定。第三方使用可能会导致名称空间冲突;他们可能会不知不觉地用自己的方法覆盖现有的私有方法,从而造成灾难性的后果。有关私有API遵循的约定的建议,请参见私有方法。

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.

但是对于现代的Objective-C运行时,我相信ivar子类中的命名冲突已被消除,因此这不再是问题。因此,我认为这就是为什么他们默认情况下使模板使用下划线前缀来匹配Apple内部代码的原因。

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.

这篇关于Xcode4模板现在在iVars上使用下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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