iOS代码规范 beta 0.1

1. 关于命名

1>要求含义清楚,尽量做到不需要注释也能了解其作用,若做不到,就加注释

2>类的命名

1.大驼峰式命名:每个单词的首字母都采用大写字母;
2.使用前缀IFS+模块+�名称+后缀的方式命名;
例:IFSHomePageViewController

3>后缀要求

1.ViewController: 使用ViewController做后缀
例:MFHomeViewController

2.View: 使用View做后缀
例:MFAlertView

3.UITableCell:使用Cell做后缀
例:MFNewsCell

4.其它类推

5>property变量命名

1.小驼峰式命名
2.适当使用控件类型缩写以表明变量所属控件类型,如:

UIButton -> Btn
UILabel -> L
UITextField -> TF
@property (nonatomic, copy) NSString *userName;
@property (nonatomic, weak) UILabel  *userNameL;
@property (nonatomic, weak) UIButton *loginBtn;

6>�常量命名

1.尽量不用#define预处理指令;
2.以字母 k 开头,后面遵循大驼峰命名;

//私有常量 
static const NSTimeInterval kAnimationDuration = 0.3; //.m定义即可

//全局数值型常量
extern const NSTimeInterval kAnimationDuration; //.h声明
const NSTimeInterval kAnimationDuration = 0.3; //.m实现

//全局�字符串型常量
extern NSString * const kEOCStringConstant; //.h声明
NSString * const kEOCStringConstant = @"value";//.m实现

7> Enum

1.Enum类型的命名与类的命名规则一致
2.Enum中枚举内容的命名需要以该Enum类型名称开头

typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
        AFNetworkReachabilityStatusUnknown = -1,
        AFNetworkReachabilityStatusNotReachable = 0,
        AFNetworkReachabilityStatusReachableViaWWAN = 1,
        AFNetworkReachabilityStatusReachableViaWiFi = 2
};

8> Delegate命名

1.以类名开头;
2.将类实例以参数的形式返回出来;
3.参考UITableViewDelegate方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

3. 关于注释

1最好的代码是不需要注释的,尽量通过合理的命名、良好的代码把含义表达清楚;
2.注释需要与代码同步更新
3.如果做不到命名尽量的见名知意的话,就可以适当的添加一些注释或者mark

1>属性注释

/** 学生 */
@property (nonatomic, strong) Student *student;

2>方法声明注释:

/**
* @brief 登录验证
*
* @param personId 用户名
* @param password 密码
* @param complete 执行完毕的block
*
* @return
*/
+ (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;

4. 关于UI布局

1.xib + autolayout
2.Masonry
3.不建议使用手写frame的方式进行控件布局;

5. 格式化代码

1>指针 "*" 位置

1.定义变量:
NSString *userName;
NSString[空格]*userName;

2.定义property:
@property (nonatomic, copy) Foo *foo;
@property[空格](nonatomic,[空格]copy)[空格]Foo[空格]*foo;

3.定义�方法:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
-[空格](id)initWithNibName:(NSString[空格]*)nibNameOrNil[空格]bundle:(NSBundle[空格]*)nibBundleOrNil;

3>代码缩进

1.使用tab键进行代码缩进
2.定期使用Editor -> Structure -> Re-Indent 功能�进行排版格式化处理;

4>对method进行分组

使用 #pragma mark - 方式对类的方法进行分组

#pragma mark - life cycle methods
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {...}
- (void)viewDidLoad {...}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {...}

#pragma mark - public methods 
- (void)samplePublicMethodWithParam:(NSString*)sampleParam {...}

#pragma mark - private methods
- (void)samplePrivateMethod {...}
- (void)sampleForIf {...}
- (void)sampleForWhile {...}
- (void)sampleForSwitch {...}
- (void)wrongExamples {...}

5>大括号写法

1.对于method: 左括号另起一行写

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
  if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
     // Custom initialization
  }
  return self;
}

2.其他�情�形: 左括号跟在第一行后边,空格+{

- (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) {
    caseSampleEnumUndefined: {
      // do something
      break;
    }
    caseSampleEnumOne: {
      // do something
      break;
    }
    caseSampleEnumTwo: {
      // do something
      break;
    }
    default: {
      NSLog(@"WARNING: there is an enum type not handled properly!");
      break;
    }
}

6>ViewController代码组织结构

#import "SomeModel.h"
#import "SomeView.h"
#import "SomeController.h"
#import "SomeStore.h"
#import "SomeHelper.h"
#import 

static NSString * const �IFSFooStringConstant = @"FoobarConstant";
static CGFloat const �IFSFooFloatConstant = 1234.5;

@interface �IFSFooViewController () <�IFSBarDelegate>
@property (nonatomic, copy, readonly) Foo *foo;
@end

@implementation IFSFooViewController

#pragma mark - Lifecycle
- (void)dealloc {...}
- (instancetype)initWithFoo:(Foo *)foo {...}
 
#pragma mark - View Lifecycle
- (void)viewWillAppear:(BOOL)animated {...}
- (void)viewDidLoad {...}

#pragma mark - Layout
- (void)makeViewConstraints {...}

#pragma mark - Public Interface
- (void)startFooing {...}
- (void)stopFooing {...}

#pragma mark - User Interaction
- (void)btnOnclick {...}

#pragma mark - Private Methods
- (NSString *)displayNameForFoo:(Foo *)foo {...}

#pragma mark - IFSBarDelegate
- (void)foobar:(Foobar *)foobar didSomethingWithFoo:(Foo *)foo {...}

@end

你可能感兴趣的:(iOS代码规范 beta 0.1)