neteaseblog objective-c style guide
TRANSCRIPT
Objective-C开发规范网易博客内部版本
基于Google、Apple公司的相关规范
jenkinv 2012/05/29
目录
命名格式约定工具
命名camel-casing(驼式命名)
尽量不使用缩写避免使用特殊字符,但实例变量允许使用_
命名
类名(Class Names)
首字母大写只有框架或者组件代码才需要类前缀(如NS,UI,IB等)
命名
分类(Category Names)
首字母大写文件名:NSString+SBJSON.h
类别名:NSStringSBJSONAdditions
命名
方法名(Method Names)
首字母小写getter不需要get-前缀,直接使用属性名称
命名
实例变量(Instance Variables)
下划线开头使用@property和@synthesize
直接使用@synthesize property=_property;,无须在构造函数中声明实例变量。
命名
常量(Constants)
k开头
格式使用4空格代替Tab
方法参数过多时,使用多行,冒号对齐 UIBarButtonItem *leftButton = [MBarButtonItem barButtonItemWithTitle:@" 博客" normalImage:leftButtonImage highligtedImage:leftButtonImagePress target:self action:@selector(restoreNavBar:)];
格式方法前的+/-后面留⼀一空格,参数列表中只有参数之间留空格
- (void)headerView:(HeaderView *)headerView selectedIndex:(NSInteger)index {
格式
协议尖括号与类名之间不留空格@interface MessageListViewController : PageViewController<LDTaskObserver> {
约定
初始化时不需要设置变量为0或者nil
约定
私有方法可以在实现文件中通过类别来实现,如EGORefreshTableHeaderView.m中包含:@interface EGORefreshTableHeaderView (Private)
- (void)setState:(EGOPullRefreshState)aState; @end
约定
#import Objective-C/Objective-C++头文件#include C/C++头文件
约定
避免在init和dealloc中使用accessors
原因:子类可能会重写accessors并操作其它成员变量
约定
对NSString对象使用copy而不要使用retain
@property (copy) NSString *name;
约定
不能直接把integer转型成BOOL,转型时只使用了 后⼀一个字节,可以使用三目运算达到同样的效果。不要直接比较BOOL与YES/NO
约定
delegate对象不能被retain
@protocols来实现回调
约定
手动管理内存如果使用的第三方组件需要arc,可以给相应的源文件添加编译选项 -fobjc-arc
工具
代码警告(Issue,cmd+4)
代码静态分析(Analyze,shift+cmd+B)代码运行时分析(Profile, cmd+I)
参考
Google Objective-C Style Guide
Apple’s Cocoa Coding Guidelines