iOS

iOS开发规范

2021-02-22  本文已影响0人  丿星纟彖彳亍

项目基础

项目新建信息

项目初始配置

项目文件结构

所有的文件应放在工程中的项目目录下。
项目文件和物理文件需保持一致。
Xcode创建的任何组(group)都必须有文件夹映射。
项目文件不仅可以按照业务类型分组,也可以根据功能分组。

代码格式规范

代码注释格式

//
//  AppDelegate.h
//  项目名称
//
//  Created by 开发者姓名 on 2018/6/8.
//  Copyright © 2018年 公司名称. All rights reserved.
//
// Framework
#import <UIKit/UIKit.h>

// Model
#import "WTUser.h"

// View
#import "WTView.h"
/**
<#Description#>

@param application <#application description#>
@param launchOptions <#launchOptions description#>
@return <#return value description#>
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

代码结构与排版

#pragma mark - Lifecycle

- (instancetype)init {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)viewDidAppear:(BOOL)animated {}
- (void)viewWillDisappear:(BOOL)animated {}
- (void)viewDidDisappear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
- (void)dealloc {}

#pragma mark - Public

- (void)refreshData {}

#pragma mark - UI

- (void)initSubViews {}

#pragma mark - Data

- (void)initData {}
- (void)constructData {}

#pragma mark - Event

- (void)clickButton:(UIButton *)button {}

#pragma mark - Private

- (CGFloat)calculateHeight {}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {}

#pragma mark - Override

- (BOOL)needNavigationBar {}

#pragma mark - Setter

- (void)setWindow:(UIWindow *)window {}

#pragma mark - Getter

- (UIWindow *)window {}
@property (strong, nonatomic) UIWindow *window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if (door.isClosed) {
        // Do something
    } else {
        // Do something
    }
    return YES;
}

一个缩进使用四个空格。
在”-“或者”+“号之后应该有一个空格,方法的大括号和其它大括号始终和声明在同一行开始,在新的一行结束,另外方法之间应该空一行。

代码命名规范

代码命名基础

常用缩写词 含义 常用缩写词 含义
app application max maximum
alt alternate min minimum
calc calculate msg message
alloc allocte rect rectangle
dealloc deallocte msg message
init initialize temp temporary
int integer func function

类和协议命名

变量和属性命名

@property (strong, nonatomic) UIWindow *window;
- (void)setWindow:(UIWindow *)window;
- (UIWindow *)window;
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;

方法和函数命名

- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
 // 后续多个参数使用with
 - (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2; 
  //  添加适当介词能够使方法的含义更明确
  - (BOOL)lockWhenCondition:(NSInteger)condition beforeDate:(NSDate *)limit;
   // 第一个参数用了with,后面的参数不使用with
    - (instancetype)initWithImage:(nullable UIImage *)image highlightedImage:(nullable UIImage *)highlightedImage;
- (void)getLineDash:(nullable CGFloat *)pattern count:(nullable NSInteger *)count phase:(nullableCGFloat *)phase;
+ (instancetype)buttonWithType:(UIButtonType)buttonType;// 类方法创建对象
+ (UIApplication *)sharedApplication;// 单例命名
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

常量和宏的命名

枚举的命名

// NS_ENUM
typedef NS_ENUM(NSInteger, UIStatusBarAnimation) {
    UIStatusBarAnimationNone    = 0,
    UIStatusBarAnimationFade    = 1,
    UIStatusBarAnimationSlide   = 2,
}
typedef NS_OPTIONS(NSUInteger, UIRemoteNotificationType) {
    UIRemoteNotificationTypeNone    = 0,
    UIRemoteNotificationTypeBadge   = 1 << 0,
    UIRemoteNotificationTypeSound   = 1 << 1,
    UIRemoteNotificationTypeAlert   = 1 << 2,
    UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3,
}

通知命名

// [相关联类名或者功能模块名] + [will/Did](可选) + [描述] + Notification
UIApplicationDidEnterBackgroundNotification       
UIApplicationWillEnterForegroundNotification      

类型别名命名

根据作用域添加前缀(含项目前缀),格式:[类名或功能模块名] + [描述]。

文件资源命名规范

// 常见类型:logol,icon,img
// 常见状态:normal,selected,highlight
UIImage *image = [UIImage imageNamed:@"wt_mine_setting_normal"];

代码警告处理

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
// 造成警告的代码
#pragma clang diagnostic pop
push/pop    Other Warning Flags
-Wformat —-> -Wno-format 
-Wunused-variable —-> -Wno-unused-variable 
-Wundeclared-selector —-> -Wno-undeclared-selector 
-Wint-conversion —-> -Wno-int-conversion

外部库文件引入

/640.jpeg

代码版本管理

版本管理工具:svn 或 git。

svn文件管理配置

目录~/.subversion打开config文件全局配置global-ignore,所有的仓库都会受到影响,而svn:ignore只影响仓库目录。

git文件管理配置

.gitignore_global为全局配置,而仓库目录下的.gitignore文件仅注明本仓库被git忽略的文件,常见语法如下:

提交信息规范

构建和分发

参考文档

上一篇 下一篇

猜你喜欢

热点阅读