美文网首页
雄牛iOS开发代码规范

雄牛iOS开发代码规范

作者: tianlei | 来源:发表于2016-03-30 14:15 被阅读103次

    1.图片全部放到 Assets.xcassets

    2.类文件夹以项目首字母缩写大写开头,通用的东西以公司缩写开头

    3.创建类时加上公司特有的类前缀

    NS 是Foundation中的类前缀 NS 是Next Step 这家公司的简称
    UI 对应是UIKit
    BF 对应的是雄牛金融


    比如创建一个图片浏览器的类

    错误示例
    PhotoBrowser
    ***************
    好的习惯
    BFPhotoBrowser
    

    4.保持良好的可阅读性,代码一行尽量不超过120个字符,对于多参数方法尽量以冒号为基准,进行对齐

    错误示范:
     - (void)addChildVC:(UIViewController *)childVC titile:(NSString *)title     normalImage:(NSString *)image selectdImage:(NSString *)selectedImage
    {//相关操作
    }
    
    - (void)addChildVC:(UIViewController *)childVC  titile:(NSString *)title
                 normalImage:(NSString *)image
        selectdImage:(NSString *)selectedImage
    
    好的习惯:
    - (void)addChildVC:(UIViewController *)childVC
              titile:(NSString *)title
         normalImage:(NSString *)image
        selectdImage:(NSString *)selectedImage
    {//相关操作
    }
    

    5..正确的宏定义和常量的命名规范

    错误示范:
    #define baseUrl @"ommo.com"
    好的习惯:
    #define BASE_URL @"ommo.com"
    

    常量的命名: 常量以小写字母k开头,后续首字母大写

    错误示范:
     const NSString *TCPPort = @"80";
    好的习惯:
     const NSString *kTCPPort = @"80";
    

    6.关于 == 相等的判断

    书写顺序

    错误示范:
     if (HTTPStatusCode == 200){
        }
    好的习惯:可以避免变成赋值操作
     if (200 == HTTPStatusCode){
        }
    

    关于布尔值

    错误示例:
    Bool isAdult;
    if (age > 18){
      isAdult = YES;
    }  else {
      isAdult = NO;
    }
    //
    好的习惯
    Bool isAdult;
    isAdult = age > 18; 
    

    7.正确的使用 空格 让代码变的更加优美 拒绝拥挤的人生

    方法+,-后,与()之间有1个空格

    错误示范:
    -(void)marryMe
    { }
    好的习惯:
    - (void)marryMe
    {  }
    

    运算符号间要留有合适的间隔

    错误示范:
    sum=value1+value2; 
    UILable*lbl=[[UILable alloc]init] ; //疯掉了
    好的习惯:
    sum = value1 + value2;//瞬间 高大上
    UILable *lbl = [[UILable alloc] init] ;
    

    字典构造时的注意点 : 前后要留有一个空格

    错误示范:
     NSDictionary *attributs = @{                        
        NSForegroundColorAttributeName:[UIColor orangeColor],
        NSFontAttributeName:[UIFont systemFontOfSize:12]
        };
    正确示例:
      NSDictionary *attributs = @{                        
        NSForegroundColorAttributeName : [UIColor orangeColor],
        NSFontAttributeName : [UIFont systemFontOfSize:12]
        };
    

    8. .h 文件中用 @class .m文件中用#import 避免重复导入

    9.写delegate的时候类型应该为weak弱引用,以避免循环引用

    错误示范:
    @property (nonatomic, strong) id <UITableViewDataSource> dataSource;
    
    正确操作:
    @property (nonatomic, weak) id <UITableViewDataSource> dataSource;
    

    10.建议使用“#pragma mark”,方便阅读代码

    #pragma mark- dataSource
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 6;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {//相关操作
    }
    
    #pragma mark- delegate
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {//相关操作
    }
    

    11.实例变量声明时变量名前面加下划线“_”,局部变量不用加

    错误示范:
    @implementation ViewController
    {
        UIButton *authCodeBtn;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *_loginBtn = [[UIButton alloc] init];
    }
    
    好的习惯:
    @implementation ViewController
    {
        UIButton *_authCodeBtn;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *loginBtn = [[UIButton alloc] init];
    }
    
    
    

    12.建议使用 NS_ENUMNS_OPTIONS 宏来定义枚举类型

    错误示范:
    typedef enum{
    
        UIButtonTypeInfoLight,
        UIButtonTypeInfoDark,
        UIButtonTypeContactAdd
    
    } UIButtonType;
    
    好的习惯:
    typedef NS_ENUM(NSInteger, UIButtonType) {
    
        UIButtonTypeInfoLight,
        UIButtonTypeInfoDark,
        UIButtonTypeContactAdd
    
    };
    

    13.控件声明时要用 weak

    错误示范:
    @property (nonatomic,strong) UILabel *lbl;
    
    正确示例:
    @property (nonatomic,weak) UILabel *lbl;
    
    在创建时
    UILable *temLbl = [[UILable alloc] init]; //不这样做会因为引用计数 为0 被释放掉
    self.lbl = temLbl;
    

    14.通知的命名规范

    通知命名规则: [触发通知的类名] + [Did | Will] + [动作] + Notification
    错误示例:
    UIKIT_EXTERN NSString *const textFieldTextBeginEditingNotification;
    UIKIT_EXTERN NSString *const textFieldTextEndEditingNotification;
    正确操作:
    UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
    UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
    

    15.正确的 if...else...

    a.书写规范

    错误示例:
    if(){
    //do somthing
    } 
    else {
    //do somthing
    }
    正确的方式:
    if(){
    //do somthing
    } else {
    //do somthing
    }
    

    b.不合理的嵌套

    错误示例:
    if(userName.length){
         if (passWord.length) {
                //可以登录
            }
    }
    好的习惯:
    if(!userName.length){ return; };
    if(!passWord.length){ return; };
    

    16.NSString、NSDictionary、NSArray和NSNumber的字面值应该在创建这些类的不可变实例时被使用。请特别注意nil值不能传入NSArray和NSDictionary字面值,因为这样会导致crash

    错误示例:
    NSArray *names = [NSArray arrayWithObjects:@"Rose",@"Jack",@"Alex", @"Steve", nil];  
    NSDictionary *productManagers = [NSDictionary dictionaryWithObjectsAndKeys: @"Kate", @"iPhone": @"Kamal", @"iPad":@"Bill", nil];  
    NSNumber *shouldScroll = [NSNumber numberWithBool:YES];  
    NSNumber *buildingStreetNumber = [NSNumber numberWithInteger:100];  
    
    正确方式:
    NSArray *names = @[@"Rose",@"Jack",@"Alex", @"Steve"];  
    NSDictionary *productManagers = @{ @"Kate" : iPhone" , @"Kamal" : @"iPad"};  
    NSNumber *shouldScroll = @YES;  
    NSNumber *buildingStreetNumber = @100; 
    

    17.异常处理命名规则

    [Prefix] + [UniquePartOfName] + Exception

    错误示例:
    FontUnavailable
    ****************
    正确示例:
    UIFontUnavailableException
    

    18.对一些相同的东西避免写死,特别是控件的frame 不便于修改

    比如有两个控件 左对齐

    错误示例:
     UITextField *phoneTf = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 100, 40)];
     [self.view addSubview:phoneTf];
     UITextField *passwordTf = [[UITextField alloc] initWithFrame:CGRectMake(10, 110, 100, 40)];
     [self.view addSubview:passwordTf];
    //
    正确的方式:
     CGFloat margin = 10;
     CGFloat width = 100;
     CGFloat height = 40;
     UITextField *phoneTf = [[UITextField alloc] initWithFrame:CGRectMake(margin, 0, width, height)];
     [self.view addSubview:phoneTf];
     UITextField *passwordTf = [[UITextField alloc] initWithFrame:CGRectMake(margin, CGRectGetMaxY(phoneTf.frame) + margin, width, height)];
     [self.view addSubview:passwordTf];    
    

    19.让方法的命名像一句话,即见名知意

    错误示范:

    - (void)chooseCard:(NSUInteger)index
    

    好的习惯:

    - (void)chooseCardAtIndex:(NSUInteger)index
    {}
    

    相关文章

      网友评论

          本文标题:雄牛iOS开发代码规范

          本文链接:https://www.haomeiwen.com/subject/dpgslttx.html