iOS代码规范(待完善)

2018-09-09  本文已影响146人  后知后觉空想家

一、命名规范

1.1 类的命名

1.2 Xib、Storyboard命名

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;

1.5 枚举的命名

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

1.6 宏的命名

#define THIS_IS_AN_MACRO @"THIS_IS_AN_MACRO"
#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作为后缀,区别在于二者信息的流方向不同

//代理
<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.tableView.delegate = self;
//不推荐
_tableView.delegate = self;

2.3 代码风格

  #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
  {...}   
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

2.4 init方法

- (instancetype)initWithName:(NSString *)name 
{
    self = [super init];
    if (self) {
        _name = name;
    }
    return self;
}

2.5 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
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

2.8 字面量语法示范

NSNumber *intNum = @1;
NSNumber *floatNum = @2.5f;
NSNumber *boolNum = @YES;
//修饰+Array
//每个元素的@对齐
NSMutalbeArray *titleArray = @[@"标题1",
                               @"标题2",
                               @"标题3"].mutableCopy;
//或直接用复数形式表示数组
NSArray *titles = @[@"标题4",
                    @"标题5",
                    @"标题6"];
//Array的数据访问
NSString *title = titleArray[index];

//修饰+dic,
//每组的:对齐
NSDictionary *userDic = @{@"nickname"   :@"张三",
                          @"age"        :@(18)
                          @"phone"      :@"18736382736"};
//可变字典       
NSMutableDictionary *newDic = @{@"test" :@"可变字典写法"}.mutableCopy;
//添加键值对
newDic[@"add"] = @"添加";
//Dictionary数据访问
NSString *nickName = userDic[@"nickname"];

2.9 判断nil或者YES/NO

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

三、其他

一致性

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

上一篇 下一篇

猜你喜欢

热点阅读