美文网首页
iOS代码规范(待完善)

iOS代码规范(待完善)

作者: 后知后觉空想家 | 来源:发表于2018-09-09 13:05 被阅读146次

    一、命名规范

    1.1 类的命名

    • 使用大驼峰式命名,每个单词首字母大写,可添加前缀
    • 后缀要求
      • ViewController

        HomeViewController
        
      • Model

        UserInfoModel
        
      • View

        RotationView
        
      • UITableViewCell

        HomeNewsCell
        

    1.2 Xib、Storyboard命名

    • Xib命名要与对应类名完全一致
    • Storyboard中每个Scene(视图控制器)的Storyboard ID要与类名一致
    Storyboard示例

    1.3 类别命名

    • 采用(类名 + 标识 + 扩展)的格式
    /*
    UIImageView:要扩展的类名
    HP:专属标识
    Web:扩展功能
    */
    UIImageView + HPWeb.h
    

    1.4 变量、属性的命名

    • 使用小驼峰法,除第一个单词外,其余单词首字母大写

    • 命名采用修饰+类型的方式,修饰词语义明显时可省略类型

    • 局部变量

    UILabel *titleLabel;        //表示标题的label,是UILable类型
    UIButton *confirmButton;    //表示确认按钮,是UIButton类型
    
    • 属性
    @property (nonatomic, copy) NSString *name;     //语义明显,省略类型
    @property (nonatomic, strong) UILabel *nameLabel;
    
    • 全局变量要以_开头
    //全局变量
    NSArray *_newsArray;
    
    • BOOL类型:命名应使语义更明显常

      • 使用is前缀
      BOOL isSelected;
      
      • 使用has前缀
      BOOL hasPrefix;
      
      • 使用enable后缀
      BOOL editEnable;
      

    1.5 枚举的命名

    • 使用大驼峰式命名
    • Enum内容的命名要以此Enum类型名称开头
    typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
          AFNetworkReachabilityStatusUnknown            = -1,
          AFNetworkReachabilityStatusNotReachable       = 0,
          AFNetworkReachabilityStatusReachableViaWWAN   = 1,
          AFNetworkReachabilityStatusReachableViaWiFi   = 2
      };
    

    1.6 宏的命名

    • 全部大写中间用_隔开(不带参数)
    #define THIS_IS_AN_MACRO @"THIS_IS_AN_MACRO"
    
    • k开头,后面遵循大驼峰命名(不带参数)
    #define kScreenWidth [UIScreen mainScreen].bounds.size.width
    
    • 小驼峰命名(带参数)
    #define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]
    
    

    1.7 方法的声明

    • -,+返回值之间留一个空格
    • 遵守小驼峰原则,参数前应包含一个描述性关键字来描述参数,
    - (void)samplePublicMethodWithParam:(NSString *)sampleParam;
    - (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height;
    

    1.8 协议命名

    协议使用DelegateDataSource作为后缀,区别在于二者信息的流方向不同

    • delegate:事件发生的时候,委托者需要通知代理(委托者-->代理)
    • dataSource:委托者需要从数据源拉取数据(数据源-->委托者)
    //代理
    <UITableViewDelegate>
    //数据源
    <UITableViewDataSource>
    

    1.9 类型和后缀简写

    类型 后缀
    UIViewController VC
    UIButton Btn
    UIImageView ImgView
    NSArray Arr
    NSMutableArray MArr
    NSDictionary Dic
    NSMutableDictionary MDic
    NSString Str
    NSMutableString MStr

    二、编码规范

    2.1 指针位置

    • 定义一个对象时,指针靠近变量,中间没有空格
    NSString *userName;
    

    2.2 属性写法

    • 属性关键字按照(原子性, 读写, 内存管理)的顺序排列
    @property (nonatomic, weak) UIImageView *imgView;
    @property (nonatomic, assign) NSInteger index;  //语义明显,省略类型
    @property (nonatomic, readonly, copy) NSString *gender;
    
    • 属性使用,推荐使用self.的方式应用类的成员变量,不推荐使用_变量名的方式
    //推荐方式
    self.tableView.delegate = self;
    //不推荐
    _tableView.delegate = self;
    

    2.3 代码风格

    • 使用#pragma mark -对方法进行分组
      #pragma mark - life cycle methods
      - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
      {...}
    
      #pragma mark - private methods
      - (void)samplePrivateMethod
      {...}
       
      - (void)sampleForIf
      {...}
       
      #pragma mark - public methods
      - (void)samplePublicMethodWithParam:(NSString *)sampleParam
      {...}   
    
    • 大括号写法:对于类的Method,左括号另起一行
    • 对于控制语句左括号不需要另起一行
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            
        }
        return self;
    }
    

    2.4 init方法

    • init方法返回的必须是instancetype,不能是id
    • 必须先实现他的[super init];
    - (instancetype)initWithName:(NSString *)name 
    {
        self = [super init];
        if (self) {
            _name = name;
        }
        return self;
    }
    

    2.5 Block写法规范

    • 调用block时需要对block判空
    - (void)confirmBtnClick
    {
        if (self.completedBlock) {
            self.completedBlock();
        }
    }
    

    2.6 ViewController写法规范

    主要是方法的分类,便于阅读代码

    #pragma mark – Life Cycle
     
    #pragma mark - Events
     
    #pragma mark – Private Methods
     
    #pragma mark - UITextFieldDelegate
     
    #pragma mark - UITableViewDataSource
     
    #pragma mark - UITableViewDelegate
     
    #pragma mark - Custom Delegates
     
    #pragma mark – Getters and Setters
    

    2.7 协议方法

    • 类的实例必须为回调方法的参数之一
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    
    • 回调方法的参数只有类自己的情况,方法名要符合实际含义, 如:
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    
    • 以类的名字开头(回调方法存在两个以上参数的情况)以表明此方法是属于哪个类的, 如:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
    • 使用did和will通知Delegate已经发生的变化或将要发生的变化, 如:
    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
    

    2.8 字面量语法示范

    • NSNumber字面量写法
    NSNumber *intNum = @1;
    NSNumber *floatNum = @2.5f;
    NSNumber *boolNum = @YES;
    
    • Array使用字面量语法
    //修饰+Array
    //每个元素的@对齐
    NSMutalbeArray *titleArray = @[@"标题1",
                                   @"标题2",
                                   @"标题3"].mutableCopy;
    //或直接用复数形式表示数组
    NSArray *titles = @[@"标题4",
                        @"标题5",
                        @"标题6"];
    //Array的数据访问
    NSString *title = titleArray[index];
    
    
    • Dictionary使用字面量语法
    //修饰+dic,
    //每组的:对齐
    NSDictionary *userDic = @{@"nickname"   :@"张三",
                              @"age"        :@(18)
                              @"phone"      :@"18736382736"};
    //可变字典       
    NSMutableDictionary *newDic = @{@"test" :@"可变字典写法"}.mutableCopy;
    //添加键值对
    newDic[@"add"] = @"添加";
    //Dictionary数据访问
    NSString *nickName = userDic[@"nickname"];
    

    2.9 判断nil或者YES/NO

    之前列的大纲,一时想不起要些什么了,后续补充
    

    三、其他

    一致性

    类名,变量名,上下文或者全局的一致性。
    相同类型或者具有相同作用的方法的命名方式应该相同或者类似。

    相关文章

      网友评论

          本文标题:iOS代码规范(待完善)

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