[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];

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值