简体   繁体   English

Cocoa中的实例变量命名约定

[英]Instance variable naming conventions in Cocoa

This question is about variable naming style in objective c and cocoa. 这个问题是关于目标c和cocoa中的变量命名风格。 I just want to stress that I'm not looking for a "right" answer, just good ideas. 我只想强调,我不是在寻找一个“正确”的答案,只是好主意。

I've read through Apple and Google's objective c style guides and I'm not really happy with either of them. 我已经阅读了Apple和Google的客观c风格指南,我对它们中的任何一个都不满意。 Apple's guide doesn't have any real style recommendations regarding instance variables vs local variables. Apple的指南没有关于实例变量和局部变量的任何真实样式建议。 In fact, the Cocoa library itself seems perfectly happy having function parameters of the exact same name as instance variables. 事实上,Cocoa库本身似乎非常高兴拥有与实例变量完全相同的函数参数。 That makes me cringe personally. 这让我个人感到畏缩。

Googles guide specifies that instance variables should be indicated with a trailing underscore. 谷歌指南指定实例变量应使用尾随下划线表示。 Alright, all well and good, but it suggests that we then synthesize every public property with @synthesize property = property_. 好吧,一切都很好,但它表明我们然后用@synthesize property = property_合成每个公共属性。 I don't know about anyone else, but I'll be damned if I'm going to do that for every instance variable in my project. 我不知道其他任何人,但如果我要为我的项目中的每个实例变量做这件事我都会被诅咒。 I think it's a wasteful and confusing solution. 我认为这是一个浪费和混乱的解决方案。

I'm tempted to go with the myX (eg "myInstanceVariable") naming style for object properties, but I have rarely seen that style in objective c. 我很想用对象属性的myX(例如“myInstanceVariable”)命名样式,但我很少在目标c中看到这种风格。

So yeah, what do you use? 所以是的,你用的是什么? Any style conventions out there I don't know about that you've found useful? 任何风格约定,我不知道你发现有用吗? Do you think function parameters with the same name as instance variables is dangerous, especially in multiple developer environments? 您是否认为与实例变量同名的函数参数是危险的,尤其是在多个开发人员环境中? Thanks guys and gals! 谢谢你们和gals!

NOTE - As many people have pointed out, my terminology was off in the OP. 注 - 正如许多人所指出的那样,我的术语在OP中没有。 Apologies if the original wording hurt the clarity, but I think the point was still clear. 如果原始措辞损害了清晰度,请道歉,但我认为这一点仍然很明确。

I tend to use non-prefixed instance variable names (note that "member variable" is a C++ism as it's suggestive of structures and classes being mainly interchangeable, which is not the case in Objective-C), and in cases where ambiguity arises, I use the Smalltalk convention of naming the parameter by its type with "a" or "an", eg: 我倾向于使用非前缀的实例变量名称(注意“成员变量”是C ++主义,因为它暗示结构和类主要是可互换的,而在Objective-C中却不是这样,并且在模糊性的情况下)出现,我使用Smalltalk约定命名参数的类型用“a”或“an”,例如:

- (void)setFoo:(SOFoo *)aFoo;
{
    foo = aFoo;
}

(of course, in modern ObjC you'd use a property for this.) (当然,在现代的ObjC中你会使用一个属性。)

Using theFoo instead of aFoo is also somewhat common; 使用theFoo而不是aFoo也有点常见; see the answers to this question . 看到这个问题的答案。

The Google convention makes sense if you're really worried about conflicts. 如果你真的担心冲突,谷歌会议是有道理的。 If you use an Xcode text macro or tool like Completion Dictionary or Accessorizer to generate your directives, it's pretty simple to adopt. 如果您使用Xcode文本宏完成字典Accessorizer之类的工具来生成指令,则采用它非常简单。

Note that the Cocoa key-value coding guidelines pretty much assume either (a) you do not prefix/suffix your instance variable names, or (b) you implement (or synthesize) non-prefixed/suffixed accessors for them. 请注意, Cocoa键值编码指南几乎假定(a)您没有为实例变量名添加前缀/后缀,或者(b)为它们实现(或合成)非前缀/后缀访问器。 As someone else mentioned, do not use the _ prefix; 正如其他人提到的,不要使用_前缀; it's reserved for Apple's use in their frameworks. 它保留给Apple在其框架中的使用。

First: there are no "member variables" in Objective-C, there are "Instance Variables" or "ivars". 第一:Objective-C中没有“成员变量”,有“实例变量”或“ivars”。

Google is NOT any kind of authority on Objective-C coding or Mac development. Google对Objective-C编码或Mac开发没有任何权威。 Google Earth is a Qt app: 'nuff said. 谷歌地球是一个Qt应用程序:'努夫说。

I seem to remember seeing an official coding style guide from Apple for Objective-C, which I'm not finding at the moment. 我似乎记得看过Apple的Objective-C官方编码风格指南,目前我还没有找到。 This article is a pretty good summary, though: 不过,本文是一个非常好的总结:

http://cocoadevcentral.com/articles/000082.php http://cocoadevcentral.com/articles/000082.php

Found it! 找到了! Here's Apple's official coding guidelines for Cocoa: 这是Apple针对Cocoa的官方编码指南:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html

In Cocoa, the style is to have pascalCased (or is that camelCased? I can never remember) names; 在Cocoa中,风格是有pascalCased(或者是camelCased?我永远不记得)的名字; and have the member variables be named the same as the accessor methods. 并且成员变量的名称与访问者方法相同。 (Such as NSInteger anInteger , - anInteger and - setAnInteger: ). (例如NSInteger anInteger- anInteger- setAnInteger:

It might not be the best style, but it's probably a good idea to get used to it if you are going to do any amount of work with Cocoa, as a number of mechanisms assume this particular kind of naming convention. 它可能不是最好的风格,但是如果你要使用Cocoa进行任何工作,那么习惯它可能是一个好主意,因为许多机制都假定这种特殊的命名约定。

m_variableName is pretty common too for member variables. m_variableName对于成员变量也很常见。 Personally, most of the time, I just go with the same name for both variables and making the distinction between this.varname and varname . 就个人而言,大多数情况下,我只是为两个变量使用相同的名称,并区分this.varnamevarname

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM