NeteaseBlog Objective-C Style Guide

21
Objective-C开发规范 网易博客内部版本 基于GoogleApple公司的 相关规范 jenkinv 2012/05/29

Transcript of NeteaseBlog Objective-C Style Guide

Page 1: NeteaseBlog Objective-C Style Guide

Objective-C开发规范网易博客内部版本

基于Google、Apple公司的相关规范

jenkinv 2012/05/29

Page 2: NeteaseBlog Objective-C Style Guide

目录

命名格式约定工具

Page 3: NeteaseBlog Objective-C Style Guide

命名camel-casing(驼式命名)

尽量不使用缩写避免使用特殊字符,但实例变量允许使用_

Page 4: NeteaseBlog Objective-C Style Guide

命名

类名(Class Names)

首字母大写只有框架或者组件代码才需要类前缀(如NS,UI,IB等)

Page 5: NeteaseBlog Objective-C Style Guide

命名

分类(Category Names)

首字母大写文件名:NSString+SBJSON.h

类别名:NSStringSBJSONAdditions

Page 6: NeteaseBlog Objective-C Style Guide

命名

方法名(Method Names)

首字母小写getter不需要get-前缀,直接使用属性名称

Page 7: NeteaseBlog Objective-C Style Guide

命名

实例变量(Instance Variables)

下划线开头使用@property和@synthesize

直接使用@synthesize property=_property;,无须在构造函数中声明实例变量。

Page 8: NeteaseBlog Objective-C Style Guide

命名

常量(Constants)

k开头

Page 9: NeteaseBlog Objective-C Style Guide

格式使用4空格代替Tab

方法参数过多时,使用多行,冒号对齐 UIBarButtonItem *leftButton = [MBarButtonItem barButtonItemWithTitle:@" 博客" normalImage:leftButtonImage highligtedImage:leftButtonImagePress target:self action:@selector(restoreNavBar:)];

Page 10: NeteaseBlog Objective-C Style Guide

格式方法前的+/-后面留⼀一空格,参数列表中只有参数之间留空格

- (void)headerView:(HeaderView *)headerView selectedIndex:(NSInteger)index {

Page 11: NeteaseBlog Objective-C Style Guide

格式

协议尖括号与类名之间不留空格@interface MessageListViewController : PageViewController<LDTaskObserver> {

Page 12: NeteaseBlog Objective-C Style Guide

约定

初始化时不需要设置变量为0或者nil

Page 13: NeteaseBlog Objective-C Style Guide

约定

私有方法可以在实现文件中通过类别来实现,如EGORefreshTableHeaderView.m中包含:@interface EGORefreshTableHeaderView (Private)

- (void)setState:(EGOPullRefreshState)aState; @end

Page 14: NeteaseBlog Objective-C Style Guide

约定

#import Objective-C/Objective-C++头文件#include C/C++头文件

Page 15: NeteaseBlog Objective-C Style Guide

约定

避免在init和dealloc中使用accessors

原因:子类可能会重写accessors并操作其它成员变量

Page 16: NeteaseBlog Objective-C Style Guide

约定

对NSString对象使用copy而不要使用retain

@property (copy) NSString *name;

Page 17: NeteaseBlog Objective-C Style Guide

约定

不能直接把integer转型成BOOL,转型时只使用了 后⼀一个字节,可以使用三目运算达到同样的效果。不要直接比较BOOL与YES/NO

Page 18: NeteaseBlog Objective-C Style Guide

约定

delegate对象不能被retain

@protocols来实现回调

Page 19: NeteaseBlog Objective-C Style Guide

约定

手动管理内存如果使用的第三方组件需要arc,可以给相应的源文件添加编译选项  -fobjc-arc

Page 20: NeteaseBlog Objective-C Style Guide

工具

代码警告(Issue,cmd+4)

代码静态分析(Analyze,shift+cmd+B)代码运行时分析(Profile, cmd+I)