含义清楚,尽量做到不需要注释也能了解其作用,若做不到,就加注释
使用全称,不适用缩写
例子:MFHomePageViewController
ViewController: 使用ViewController做后缀
例子: MFHomeViewController
View: 使用View做后缀
例子: MFAlertView
UITableCell:使用Cell做后缀
例子: MFNewsCell
Protocol: 使用Delegate或者DataSource作为后缀
例子: UITableViewDelegate
UI控件依次类推
例子:firstName、lastName
例子:NSString * _somePrivateVariable
例子:///注释
@property (nonatomic, copy) NSString *userName;
例子: #define THIS_IS_AN_MACRO @"THIS_IS_AN_MACRO"
例子:#define kWidth self.frame.size.width
#define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]
Enum类型的命名与类的命名规则一致
Enum中枚举内容的命名需要以该Enum类型名称开头
例子:
1 typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
2 AFNetworkReachabilityStatusUnknown = -1,
3 AFNetworkReachabilityStatusNotReachable = 0,
4 AFNetworkReachabilityStatusReachableViaWWAN = 1,
5 AFNetworkReachabilityStatusReachableViaWiFi = 2
6 };
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath;
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
1> 声明位置
在.m文件中最上方,定义空的category进行声明
例子:
1 #import "CodeStandardViewController.h"
2 // 在这个category(类目)中定义变量和方法
3 @interface CodeStandardViewController ()
4 {
5
6 // 声明私有变量
7 }
8
9 // 私有方法
10 - (void)samplePrivateMethod;
11 @end
12
13 @implementation CodeStandardViewController
14 // 私有方法的实现
15 - (void)samplePrivateMethod
16 {
17 //some code
18 }
最好的代码是不需要注释的 尽量通过合理的命名
良好的代码把含义表达清楚 在必要的地方添加注释
注释需要与代码同步更新
如果做不到命名尽量的见名知意的话,就可以适当的添加一些注释或者mark
例子:
/// 学生
@property (nonatomic, strong) Student *student;
1 /** 2 * @brief 登录验证 3 * 4 * @param personId 用户名 5 * @param password 密码 6 * @param complete 执行完毕的block 7 * 8 * @return 9 */ 10 + (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;
使用Interface Builder进行界面布局
Xib文件的命名与其对应的.h文件保持相同
Xib文件中控件的组织结构要合理,Xib文件中控件需要有合理的可读性强的命名,方便他人理解
定义一个对象时,指针 "*" 靠近变量
例子: NSString *userName;
在 - 、+ 和 返回值 之间留一个空格,方法名和第一个参数之间不留空格
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
{...}
例子:
CGFloatoringX = frame.origin.x; CGFloatoringY = frame.origin.y; CGFloatlineWidth = frame.size.width;
例子:
1 #pragma mark - private methods
2
3 - (void)samplePrivateMethod
4 {...}
5
6 - (void)sampleForIf
7 {...}
使用 #pragma mark - 方式对类的方法进行分组
例子:
1 #pragma mark - private methods
2
3 - (void)samplePrivateMethod
4 {...}
5
6 - (void)sampleForIf
7 {...}
8
9 - (void)sampleForWhile
10 {...}
11
12 - (void)sampleForSwitch
13 {...}
14
15 - (void)wrongExamples
16 {...}
17
18 #pragma mark - public methods
19 - (void)samplePublicMethodWithParam:(NSString*)sampleParam
20 {...}
21
22 #pragma mark - life cycle methods
23 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
24 {...}
25
26 - (void)viewDidLoad
27 {...}
28
29 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
30 {...}
例子:
1 - (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
2 {
3 self = [super initWithNibName:nibNameOrNil
4
5 bundle:nibBundleOrNil];
6
7 if (self) {
8 // Custom initialization
9 }
10
11 return self;
12 }
例子:
1 - (void)sampleForIf
2 {
3 BOOL someCondition = YES;
4 if(someCondition) {
5 // do something here
6 }
7 }
8 - (void)sampleForWhile
9 {
10 int i = 0;
11 while (i < 10) {
12 // do something here
13 i = i + 1;
14 }
15 }
16 - (void)sampleForSwitch
17 {
18 SampleEnum testEnum = SampleEnumTwo;
19 switch(testEnum) {
20 caseSampleEnumUndefined:{
21 // do something
22 break;
23 }
24 caseSampleEnumOne:{
25 // do something
26 break;
27 }
28 caseSampleEnumTwo:{
29 // do something
30 break;
31 }
32 default:{
33 NSLog(@"WARNING: there is an enum type not handled properly!");
34 break;
35 }
36 }
错误示例:
原文:http://www.cnblogs.com/lishishi/p/5495032.html