iOS 开发技术之经验总结篇

iOS代码规范 (好代码其实不如“好看”的代码^^)

2019-06-15  本文已影响0人  灯泡虫

编写的公司iOS端代码规范 (已经脱敏了)

注释规范

页面规范

属性规范 (属性方面,主要关注 .h 暴露出来的属性。)

array
dataArray
interfaceDataArray(**最优,能一眼就看出该数组的作用**)
@property空格(nonatomic,空格readwrite,空格strong)空格NSArray空格*interfaceDataArray;

函数规范

- (void)aaa {

}
- (instancetype)init {
    return [self initWithBaseURL:nil];
}

- (instancetype)initWithBaseURL:(NSURL *)url {
    return [self initWithBaseURL:url sessionConfiguration:nil];
}

- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
    return [self initWithBaseURL:nil sessionConfiguration:configuration];
}

- (instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {
......
}

宏定义规范

#define BC_UDF_xxxxx @"BC_UDF_xxxxx"//购买专辑成功
#define BC_NOTI_aaaaa @"BC_NOTI_aaaaa"//节目列表页面购买成功
#define BC_K_bbbbb @"2"//每日荐听
#define BC_K_GetSum(a,b) a + b
BC_K_GetSum(1,2) * 5 //11
    
//所以应该加上括号
#define BC_K_GetSum(a,b) (a + b)
BC_K_GetSum(1,2) * 5 //得到想要的15
#ifdef DEBUG
#define NSLog(...) NSLog(__VA_ARGS__)
#define debugMethod() NSLog(@"%s", __func__)
#else
#define NSLog(...)
#define debugMethod()
#endif
#define WeakObj(o) autoreleasepool{} __weak typeof(o) o##Weak = o;
#define StrongObj(o) autoreleasepool{} __strong typeof(o) o = o##Weak;

//例如
@WeakObj(self);
self.block =  ^{
    @StrongObj(self);
    [self xxxx];
}];

枚举规范

内存规范

布局规范

资源规范

第三方库规范

# Uncomment the next line to define a global platform for your project
platform :ios, '8.0'
target 'xxxx' do
    # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
    # use_frameworks!
    # Pods for xxx
    pod 'AFNetworking', '3.2.0'         #网络
    pod 'MBProgressHUD', '1.1.0'        #提示框
    ....
    target 'xxxTest' do
    inherit! :search_paths
    # Pods for testing
    end

    target 'xxxUITests' do
    inherit! :search_paths
    # Pods for testing
    end
end

MVVM规范

graph LR
A(View/ViewController)-->|持有|B(ViewModel)
B-->|持有|C(Model)
B---|不引入|D(UIKit框架)

解析规范

- (xxxxActivityModel *)xxxxModelWithResp:(NSDictionary *)response {
    NSDictionary *activityInfo = [response objectForKey:@"bbbb"];
    xxxxActivityModel *activityModel = [ParseToModel parseDictionary:activityInfo byClassName:@"xxxxActivityModel"];
    activityModel.aaa = [[response objectForKey:@"aaaa"] longLongValue];
    return activityModel;
}
NSString *xxxx = [hotBook bc_jsonString:@"xxxx"]?[hotBook bc_jsonString:@"xxxx"]:@"";
NSString *aaaa = [hotBook bc_jsonString:@"aaaa"]?[hotBook bc_jsonString:@"aaaa"]:@"";
return @{@"xxxx":xxxx, @"aaaa":aaaa, @"cccc": @"c"};

跳转规范

ps:喜欢的点个赞哈,或者赞赏支持~

上一篇 下一篇

猜你喜欢

热点阅读