标签:
1.1统一要求
1.2类的命名
1.3私有变量
1.4 property变量
1.5宏命名
1.6 Enum
1 typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) { 2 AFNetworkReachabilityStatusUnknown = -1, 3 AFNetworkReachabilityStatusNotReachable = 0, 4 AFNetworkReachabilityStatusReachableViaWWAN = 1, 5 AFNetworkReachabilityStatusReachableViaWiFi = 2, 6 };
1.7 Delegate命名
2.1声明位置
在.m文件中最上方,定义空的category进行声明
例子:
1 #import "CodeStandardViewController.h" 2 //define your private variables and methods here 3 @interface CodeStandardViewController () 4 { 5 } 6 - (void)samplePrivateMethod; 7 @end 8 9 #define THIS_IS_AN_SAMPLE_MACRO @"THIS_IS_AN_SAMPLE_MACRO" 10 @implementation CodeStandardViewController 11 #pragma mark - private methods 12 - (void)samplePrivateMethod 13 { 14 //some code 15 }
最好的代码是不需要注释的,尽量通过合理的命名,良好的代码把含义表达清楚,在必要的地方添加注释。
注释需要与代码同步更新。
如果做不到命名尽量的见名知意的话,就可以适当的添加一些注释或者mark。
3.1 属性注释例子:
/// 学生
@property (nonatomic, strong) Student *student;
3.2 方法声明注释:
/**
* @brief 登录验证
*
* @param personId 用户名
* @param password 密码
* @param complete 执行完毕的block
*
* @return
*/
+ (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;
4.1指针 "*" 位置
例子: NSString *userName;
4.2方法的声明和定义
在 - 、+ 和返回值之间留一个空格,方法名和第一个参数之间不留空格
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
...
}
4.3代码缩进
CGFloat oringX = frame.origin.x; CGFloat oringY = frame.origin.y; CGFloat lineWidth = frame.size.width;
#pragma mark - private methods - (void)samplePrivateMethod{ ... } - (void)sampleForIf{ ... }
4.4对method进行分组
使用 #pragma mark - 方式对类的方法进行分组
例子:
#pragma mark - private methods - (void)samplePrivateMethod{ ... } - (void)sampleForIf{ ... } - (void)sampleForWhile{ ... } - (void)sampleForSwitch{ ... } - (void)wrongExamples{ ... } #pragma mark - public methods - (void)samplePublicMethodWithParam:(NSString*)sampleParam{ ... } #pragma mark - life cycle methods - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ ... } - (void)viewDidLoad{ ... } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{ ... }
4.5大括号写法
例子:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; }
- (void)sampleForIf { BOOL someCondition = YES; if (someCondition) { // do something here } } - (void)sampleForWhile { int i = 0; while (i < 10) { // do something here i = i + 1; } } - (void)sampleForSwitch { SampleEnum testEnum = SampleEnumTwo; switch (testEnum) { case SampleEnumUndefined:{ // do something break; } case SampleEnumOne:{ // do something break; } case SampleEnumTwo:{ // do something break; } default:{ NSLog(@"WARNING: there is an enum type not handled properly!"); break; } }
标签:
原文地址:http://www.cnblogs.com/fearlessyyp/p/5524678.html