Objective-c 命名规范
apple document:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html
google objective-c style guide:http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml
免责声明:本文为个人学习笔记,如果翻译有误欢迎指正,但不负任何责任。如要转载,请包含这一句话。谢谢:)
1、方法和变量名字的首字母大写,不允许以下划线开头来标志私有变量,因为苹果底层api会使用这种方式命名私有变量,容易发生命名冲突。
2、名字要具有明确性与一致性,即需要写全名字,缩写尽量不要使用,即使使用,也只参考几个非常非常常用的:
alloc |
Allocate |
alt |
Alternate |
app |
Application. For example, NSApp the global application object. However, “application” is spelled out in delegate methods, notifications, and so on. |
calc |
Calculate |
dealloc |
Deallocate |
func |
Function. |
horiz |
Horizontal. |
info |
Information |
init |
Initialize (for methods that initialize new objects) |
int |
Integer |
max |
Maximum |
min |
Minimum |
msg | |
nib |
Interface Builder archive |
pboard |
Pasteboard (but only in constants) |
rect |
Rectangle |
Rep |
Representation (used in class name such as NSBitmapImageRep) |
temp |
Temporary |
vert |
Vertical |
a: 方法名字首字母大写,之后嵌入的单词首字母大写(即驼峰法),切勿使用前缀
其中两种情况是违反此条规定但合法的是:非常常见的首字母缩写词作为方法命长例如,PDF或者私有方法中使用前缀来标记群组或作区别的使用。
b:对于那些代表对象动作的的方法,使用动词作为方法的开头,但尽量避免使用do和does这种助动词,因为无法明确体现出具体什么动作的含义。
- (void)invokeWithTarget:(id)target; |
- (void)selectTabViewItem:(NSTabViewItem *)tabViewItem |
c:不要使用get来代表方法的返回值含义,直接用它的名称即可
|
right |
|
wrong |
|
wron |
d:在所有的参数前使用关键字
|
right |
|
wrong |
(我理解的可能有异,写下英文Make the word before the argument describe the argument.)
|
right |
|
wrong |
|
NSView |
|
NSMatrix, a subclass of NSView |
|
right |
|
wrong |
|
NSWorkspace |