Objective-c 命名规范

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

Message

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

3、方法命名规范:

a: 方法名字首字母大写,之后嵌入的单词首字母大写(即驼峰法),切勿使用前缀

其中两种情况是违反此条规定但合法的是:非常常见的首字母缩写词作为方法命长例如,PDF或者私有方法中使用前缀来标记群组或作区别的使用。

b:对于那些代表对象动作的的方法,使用动词作为方法的开头,但尽量避免使用do和does这种助动词,因为无法明确体现出具体什么动作的含义。

- (void)invokeWithTarget:(id)target;
- (void)selectTabViewItem:(NSTabViewItem *)tabViewItem

c:不要使用get来代表方法的返回值含义,直接用它的名称即可

- (NSSize)cellSize;

right

- (NSSize)calcCellSize;

wrong

- (NSSize)getCellSize;

wron

d:在所有的参数前使用关键字

- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;

right

- (void)sendAction:(SEL)aSelector :(id)anObject :(BOOL)flag;

wrong

e: 使用单词去修饰参数构成方法名称,而不是用参数的单词去做形容成分

(我理解的可能有异,写下英文Make the word before the argument describe the argument.)

- (id)viewWithTag:(int)aTag;

right

- (id)taggedView:(int)aTag;

wrong

f: 如果你重写继承的某个方法,在其后尽量添加新的关键字来增添参数

- (id)initWithFrame:(NSRect)frameRect;

NSView

- (id)initWithFrame:(NSRect)frameRect mode:(int)aMode cellClass:(Class)factoryId numberOfRows:(int)rowsHigh numberOfColumns:(int)colsWide;

NSMatrix, a subclass of NSView

g:不要使用and来连接方法属性类参数

- (int)runModalForDirectory:(NSString *)path file:(NSString *) name types:(NSArray *)fileTypes;

right

- (int)runModalForDirectory:(NSString *)path andFile:(NSString *)name andTypes:(NSArray *)fileTypes;

wrong

但如果方法形容了两种相区分的动作,可以使用and去连接

- (BOOL)openFile:(NSString *)fullPath withApplication:(NSString *)appName andDeactivate:(BOOL)flag;

NSWorkspace








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值