2020-04-09

2020-04-09  本文已影响0人  年少轻狂不言弃

平安健康 iOS 代码规范

1. pragma mark标注方式


#pragma mark - Life Cycle
#pragma mark - Override
#pragma mark - Public Method
#pragma mark - Private Method
#pragma mark - Request
#pragma mark - Event Response

#pragma mark - Delegate
#pragma mark UIScrollViewDelegate (系统)
#pragma mark Custom Delegate
#pragma mark PHEmptyTipViewDelegate(私有)
#pragma mark - Getter && Setter
#pragma mark - Builder

2. if/else


1). 换行格式

if (user.isHappy) {
    //Do something
} else {
    //Do something else
}

2). if: 条件比较复杂时 单独抽一个方法

if ([self judgeConditionFor]) {
         
}

- (BOOL)judgeConditionFor {
    return cond1 && cond2 || cond3;
}

3). if: 最多嵌套2层 不要超过三层

 if ([someOther boolValue]) {
       //Do something important 
     if ([someOther boolValue]) {
       //Do something important 
     }
  }

3. 方法声明尽量用泛型


@property (nonatomic, strong) NSArray <NSArray<PHDebugQuickEnterItem *> *> *dataSource;

4. 命名:驼峰式


UIButton *settingsButton;

5. 方法名换行风格


1)'-/+'和返回类型直接要有空格 e.g: - (NSInteger)

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

6. 枚举不应该加前缀k,以PH开头命名


枚举: 前缀不带'k'

typedef NS_ENUM(NSInteger, PHClaimAppealType) {
    PHClaimAppealTypeChecking = 1, 
    PHClaimAppealTypeFinish 
};

不要这样写!!!

enum GlobalConstants { 
    kMaxPinSize = 5, 
    kMaxPinCount = 500,
};

7. 常量定义方式 note: 注意顺序


.m文件

static const CGFloat kTitleSubtitleMargin = 8.f;

8. 属性定义方式&顺序


@property (nonatomic, assign, getter=isRead, readwrite) Bool read;

9. 外部属性暴露


.h
@property (nonatomic, strong, readonly) UIButton *actionButton;
.m 
@property (nonatomic, strong, readwrite) UIButton *actionButton;

10.全局变量、属性初始化位置


1) 属性尽量用懒加载初始化

- (PHGetWalletUrlAPIManager *)getWalletUrlAPIManager
{
    if (!_getWalletUrlAPIManager) {
        _getWalletUrlAPIManager = [[PHGetWalletUrlAPIManager alloc] init];
        _getWalletUrlAPIManager.delegate = self;
    }
    return _getWalletUrlAPIManager;
}

2) 严禁在viewWillAppear()、viewDidAppear()初始化全局变量和属性!!!

- (void)viewDidAppear:(BOOL)animated
{
    self.popupArray = [NSMutableArray array];
}

11. 方法参数大于等于3 参数冒号对齐


- (void)openUpNativeRoute:(PHNativeRoute)nativeRoute
               parameters:(nullable NSDictionary *)parameters
                navigator:(UINavigationController *)navigator
                 animated:(BOOL)animated
                  prepare:(nullable PHOpenPrepare)prepare
               completion:(nullable PHOpenCompletion)completion

12. 方法调用是优先考虑点语法


self.name

13.switch语句


1) 大括号'{ }' 包裹整个case

 switch (condition) {
        case 1: { // ... // Multi-line example using braces
            break;
            
        }
        case 2: { // ... // Multi-line example using braces
            break;
            
        }
        case 3: { // ... // Multi-line example using braces
            break;
            
        }
        default: // ... break;
            
    }

2) 当在switch使用枚举类型时,default是不需要的。例如:

PHClaimAppealType appealType = PHClaimAppealTypeChecking;;
switch (menuType) {
    case PHClaimAppealTypeChecking:
        // ...
        break;
    case PHClaimAppealTypeFinish:
        // ...
        break;
}

14. 图片命名规范


home_icon40x40@2x.png
图片命名
模块_类别_功能_状态.png
tab_button_search_normal.png

15. 文件import 顺序


#import "PHInstruranceMeasureItemCell.h" -本类

#import "Mode1.h" -M
#import "Mode2.h" -M
#import "Mode3.h" -M

#import "PHView1.h" -V
#import "PHView2.h" -V
#import "PHView3.h" -V
#import "PHView3.h" -V

#import "PHViewController1.h" -C
#import "PHViewController2.h" -C
#import "PHViewController3.h" -C

#import "PHAppTheme.h"  - helper, manager
#import "PHScreemSize.h"

#import "UIView + size.h" - 分类

#import <UIKit/xxxx.h> - 系统库

#import <PHNativeRouter/PHNativeRouter.h>  - pod库
#import <AFNetworking/AFNetworking.h>  - 三方库

16. 第三方库接入功能、依赖库、权限说明


第三方库接入功能、依赖库、依赖权限需在三方库依赖规范中添加说明

上一篇 下一篇

猜你喜欢

热点阅读