[iOS] Objective C 编程规范

[iOS] Objective C 编程规范

格式化代码

1.指针“*”号的位置?如:NSString *varName;

贴近变量名称,一行只能定义一个变量,不允许定义多个变量。

2. 空格VS tabs?

使用tab,一个tab设定为4个空格

3.每行的长度?

每行最多不得超过100个字,不是古老的80个字符。Xcode => Preferences => TextEditing => Page Guide at column /输入 100即可

 

方法的声明和定义

1.在- OR +和返回值之间留1个空格,方法名和第一个参数间不留空格。如:

- (void)doSomethingWithString:(NSString *)theString

{

...

}

标识参数类型的星号前面有个space。

2.当参数过长时,每个参数占用一行,以冒号对齐。如:

- (void)doSomethingWith:(GTMFoo *)theFoo

         rect:(NSRect)theRect

         interval:(float)theInterval

{

...

}

3. 方法的调用? 

调用方法沿用声明方法的习惯。

4.@public和@private

权限控制符@public和@private不缩进,顶着左边写。

5.Protocols

型标示符、代理名称、尖括号间不留空格。

@interface MyProtocoledClass : NSObject<NSWindowDelegate>

{

@private

id<MyFancyDelegate> _delegate;

}

- (void)setDelegate:(id<MyFancyDelegate>)aDelegate;

@end

6. 异常

每个异常标签的@和开括号({)分开两行写,同样适用于@catch语句。

@try

{

foo();

}

@catch (NSException *ex)

{

bar(ex);

}

@finally

{

baz();

}

7. Blocks

Blocks are preferred to the target-selector pattern when creating callbacks, as it makes code easier to read. Code inside blocks should be indented four spaces.

There are several appropriate style rules, depending on how long the block is:

  • If the block can fit on one line, no wrapping is necessary.
  • If it has to wrap, the closing brace should line up with the first character of the line on which the block is declared.
  • Code within the block should be indented four spaces.
  • If the block is large, e.g. more than 20 lines, it is recommended to move it out-of-line into a local variable.
  • If the block takes no parameters, there are no spaces between the characters ^{. If the block takes parameters, there is no space between the ^( characters, but there is one space between the ) { characters.
  • Two space indents inside blocks are also allowed, but should only be used when it's consistent with the rest of the project's code.
// The entire block fits on one line.[operation setCompletionBlock:^{ [self onOperationDone]; }];// The block can be put on a new line, indented four spaces, with the// closing brace aligned with the first character of the line on which// block was declared.[operation setCompletionBlock:^{ [self.delegate newDataAvailable];}];// Using a block with a C API follows the same alignment and spacing// rules as with Objective-C.dispatch_async(fileIOQueue_, ^{ NSString* path = [self sessionFilePath]; if (path) { // ... }});// An example where the parameter wraps and the block declaration fits// on the same line. Note the spacing of |^(SessionWindow *window) {|// compared to |^{| above.[[SessionService sharedService] loadWindowWithCompletionBlock:^(SessionWindow *window) { if (window) { [self windowDidLoad:window]; } else { [self errorLoadingWindow]; } }];// An example where the parameter wraps and the block declaration does// not fit on the same line as the name.[[SessionService sharedService] loadWindowWithCompletionBlock: ^(SessionWindow *window) { if (window) { [self windowDidLoad:window]; } else { [self errorLoadingWindow]; } }];// Large blocks can be declared out-of-line.void (^largeBlock)(void) = ^{ // ...};[operationQueue_ addOperationWithBlock:largeBlock];

 

posted on 2014-01-20 16:47  金玉游龙  阅读(121)  评论(0编辑  收藏  举报

导航