iOS Developer

工作总结 | iOS规范补充

2019-04-04  本文已影响42人  清無

Pod update注意

JSONSerialization

            BOOL validate = [NSJSONSerialization isValidJSONObject:parament];
            if (!validate) {
                // 对不是合法的JSON对象错误进行处理
                return;
            }
            NSError *error = nil;
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parament options:NSJSONWritingPrettyPrinted error:&error];
            if (error) {
                // 对数据转换错误进行处理
                return;
            }

合法JSON对象满足:

  • Top level object is an NSArray or NSDictionary
  • All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull
  • All dictionary keys are NSStrings
  • NSNumbers are not NaN or infinity

补充一些代码规范、开发约定

// if else
    BOOL flag = YES;
    if (flag) {
        
    } else {
        
    }

// if else if
    BOOL flag = YES;
    BOOL elseIfFlag = (1+1-1+2 == 5);
    if (flag) {
        
    }
// 这里换行书写
    else if(elseIfFlag) {
        
    }
/// 懒加载方式-内部配置
@property(nonatomic, strong)UIView *redView;
/// 统一初始化
@property(nonatomic, strong)NSMutableArray *dataSourceArray;

/// 统一数据初始化
- (void)initData{
    _dataSourceArray = [NSMutableArray new];
}

/// 懒加载
- (UIView *)redView{
    if (!_redView) {
        _redView = [UIView new];
        _redView.backgroundColor = UIColor.redColor;
    }
    return _redView;
}
_dataSourceArray = [@[@"1", @"2"] mutableCopy];
_parameters = [@{@"action": @"add", @"id": @"22"} mutableCopy];

// X: 不推荐这样做
_dataSourceArray = [NSMutableArray new];
[_dataSourceArray addObject:@"1"];
[_dataSourceArray addObject:@"2"];

_parameters = [NSMutableDictionary new];
[_parameters setValue:@"add" forKey:@"action"];
[_parameters setValue:@"22" forKey:@"id"];
// TALPlayer+LogReport.h

/// 加载播放器
- (void)logReport_loadPlayer;
/// 开始播放
- (void)logReport_startPlay;
// TALPlayer+InternalProperty.h

- (TALPlayerLogModel *)internalProperty_logModel;
- (TALPlayerStaticsModel *)internalProperty_staticsModel;


// TALPlayer+InternalProperty.m

- (TALPlayerLogModel *)internalProperty_logModel{
    // 这里为方便以后调试断点用,建议拆开2行写
    id value = [self valueForKey:@"logModel"];
    return value;
}

// TALPlayer+LogReportDataInfoKeys.h

/// action
extern NSString *const LogReportDataInfoActionKey;
/// 心跳
extern NSString *const LogReportDataInfoActionHeartBeatKey;
/// 严重卡顿
extern NSString *const LogReportDataInfoActionSeriousBufferKey;


// TALPlayer+LogReportDataInfoKeys.m

// action
NSString *const LogReportDataInfoActionKey = @"action";
/// 心跳
NSString *const LogReportDataInfoActionHeartBeatKey = @"heartbeat";
/// 严重卡顿
NSString *const LogReportDataInfoActionSeriousBufferKey = @"seriousbuffer";


// 使用
#import "TALPlayerLogReportDataInfoKeys.h"

NSMutableDictionary *info = [NSMutableDictionary new];
info[LogReportDataInfoActionKey] = LogReportDataInfoActionHeartBeatKey;
// info[XXXKey] = value;
// XXX.h

#pragma mark - Protocol

#pragma mark - Properties

#pragma mark - Methods

// XXX.m

#pragma mark - Consts

#pragma mark - UI Components

#pragma mark - Data Properties

#pragma mark - Initial Methods

#pragma mark - Lifecycle Methods

#pragma mark - Override Methods

#pragma mark - Public Methods

#pragma mark - Private Methods

#pragma mark - XXXDelegate

#pragma mark - Getters

#pragma mark - Setters

如上相关#pragma字符在Xcode中的自动配置,有机会我会单独分享给大家。
Xcode FileTemplate路径:Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/Cocoa Touch Class.xctemplate/

//MARK: mark here(类似于#pragma mark,只不过没有横线)

//TODO: todo here

//FIXME: fix me later

//???: What is this shit

//!!!: DO NOT TOUCH MY CODE

说明:

/**
 * 这是一个demo方法
 * @param param1 第一个参数
 * @param param2 第二个参数
 * @return BOOL值
 */
 - (BOOL)thisIsADemoFuncWithParam1: (NSString *)param1
                            param2: (NSInteger)param2{
    return NO;
}
  • 如果方法是功能性的,处理某些事件、计算、数据处理等的私有方法,则可定义方法名为handleXXX:,如-handleRedBtnClick:-handleResponsedData:-handlePlayerEvent:等;

  • 对于一些需要暴露的公有方法,则命名最好按照n的功能命名,如对于一个TALPlayer它可以playstopresume等;

  • 对于可以switch两种状态切换的状态方法,最好命名为toggleXXX:(BOOL)on,如- (void)toggleMute:(BOOL)on

typedef enum : NSUInteger {
    TALPlayerEventA = 0,
    TALPlayerEventB,
    TALPlayerEventC,
} TALPlayerEvent;
// 或者
typedef NS_ENUM(NSUInteger, MyEnum) {
    MyEnumValueA,
    MyEnumValueB,
    MyEnumValueC,
};
上一篇 下一篇

猜你喜欢

热点阅读