iOS开发代码规范

2017-09-30  本文已影响11人  e85a0a8a9ba4

一. 关于命名

  1. 统一要求
  1. 类的命名

ViewController: 使用ViewController做后缀

例子: MFHomeViewController

View: 使用View做后缀

例子: MFAlertView

UITableCell:使用Cell做后缀

例子: MFNewsCell

Protocol: 使用Delegate或者DataSource作为后缀

例子: UITableViewDelegate

其他系统UI控件依次类推
3.私有变量

例子:firstName、lastName
正确例子:NSString  *somePrivateVariable
错误例子:NSString  *_somePrivateVariable

4.property变量

例子:@property (nonatomic, copy) NSString *userName;//注释

5.宏命名

例子: #define THIS_IS_AN_MACRO    @"THIS_IS_AN_MACRO"
例子:#define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]
  1. Enum(枚举)
例子:
 typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus)
{
       AFNetworkReachabilityStatusUnknown = -1,
       AFNetworkReachabilityStatusNotReachable = 0,
       AFNetworkReachabilityStatusReachableViaWWAN = 1,
       AFNetworkReachabilityStatusReachableViaWiFi = 2
};
  1. Delegate命名
例子:
-(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. 声明位置
例子:
 #import "CodeStandardViewController.h"
// 在这个category(类目)中定义变量和方法
@interface CodeStandardViewController ()
{
     // 声明私有变量
}
  
// 私有方法
- (void)samplePrivateMethod;
@end
   
@implementation CodeStandardViewController
  // 私有方法的实现
- (void)samplePrivateMethod
{
         //some code
}

三.关于注释

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;

四. 关于UI布局

1.xib布局

五. 格式化代码

1.指针 "" 位置
定义一个对象时,指针 "
" 靠近变量

例子: NSString *userName;
  1. 方法的声明和定义
    在 - 、+ 和 返回值 之间留一个空格,方法名和第一个参数之间不留空格
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
{
}

3.代码缩进

例子:
CGFloatoringX = frame.origin.x;
CGFloatoringY = frame.origin.y;
CGFloatlineWidth = frame.size.width;
例子:
 #pragma mark - private methods 
- (void)samplePrivateMethod
{
...
}

- (void)sampleForIf
{
...
}
例子:
#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
{
...
}

5.大括号写法

例子:
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(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) {
           caseSampleEnumUndefined:{
               // do something
               break;
           }
           caseSampleEnumOne:{
               // do something
               break;
           }
           caseSampleEnumTwo:{
               // do something
               break;
           }
           default:{
               NSLog(@"WARNING: there is an enum type not handled properly!");
               break;
           }
}
错误示例:
- (void)wrongExamples
{
       BOOLsomeCondition = YES;
       if (someCondition)
           NSLog(@"this is wrong!!!");
       while(someCondition)
           NSLog(@"this is wrong!!!");
}
上一篇下一篇

猜你喜欢

热点阅读