类和函数声明:
- 类名大驼峰、函数名小驼峰
- 所有大括号前有1个空格, 跟随在当前类或函数名之后, 不换行
- 类和函数要写注释 , 使用 /// 或 /** */来注释
- 方法上边需要以后1行间隔. 《所有间隔不能超过1行》
/// 我的
class MyClass {
/**
* 添加
* @param value 参数1
*/
private func add(value: Int) {
// code
}
/// 方法一
func myMethod1() {
// code
}
}
错误方式: 没写注释, 冒号后没空格, 大括号前没有空格
func add(value:Int){
}
func myMethod1()
{
}
尽可能使用private、fileprivate来限制作用域
fileprivate class MyClass {
private func add() {
}
}
冒号, 逗号:
- 所有(冒号 “:” 逗号“,”) 前面不加空格, 后面须加1个空格
class MyClass {
private lazy var titleBtn: UIButton = configure(.init()) {
$0.setTitle("标题按钮", for: .normal)
}
private let array: [Int] = [1, 2, 3]
func method1(value1: Int) {
// code
}
}
self非必要时不使用:
class MyClass: UIViewController {
private lazy var titleBtn: UIButton = configure(.init()) {
$0.setTitle("标题按钮", for: .normal)
}
func method1(value1: Int) {
// 正确
view.addSubview(titleBtn)
// 错误
view.addSubview(self.titleBtn)
}
}
属性声明:
- 属性命名使用小驼峰
- 各个组件命名后缀带组件类型, 如UILabel组件命名 xxxLab
- 属性声明统一使用configure
- 按钮UIButton的点击事件在属性中声明, 点击事件统一命名 actionXxxx
- 属性之间无需空格
class MyClass {
private let titleLab: UILabel = configure(.init()) {
$0.text = "标题名"
}
private lazy var titleBtn: UIButton = configure(.init()) {
$0.setTitle("标题按钮", for: .normal)
$0.addTarget(self, action: #selector(titleAction), for: .touchUpOutside)
}
private let titleImage: UIImageView = configure(.init()) {
$0.contentMode = .scaleAspectFill
}
}
闭包:
- 内部使用$0来调用组件属性
let titleLab: UILabel = configure(.init()) {
$0.text = "标题名"
}
titleLab.snp.makeConstraints {
$0.edges.equalToSuperview()
}
- 弱引用的判断命名使用 _self , 不能强制解包 self!
var closure = {[weak self] in
guard let _self = self else {
return
}
// 错误方式
self!.array = []
}
- 高级运算符,简单的闭包一行显示
let tempArray = arr.filter { $0 == 1 }
解包方式:
正确方式
if let value = optional {
// code
}
if let value = optionalValue as? String {
// code
}
错误方式
let value = optional!
let value = optionalValue as! String
运算符、操作符前后都要有空格:
let count = 1 + 2
let status = 1 ..< 2
func add() -> Int {
return 1 + 2
}
注释:
- 注释写在代码上边
- 函数注释 /// 或 /** */ , 函数内部作用域使用 //
/// 方法一
func method1(value1: Int) {
// 相加
let count = 1 + 2
}
/**
* 添加
* @param value 参数1
* @param value2 参数2
*/
private func method2(value: Int, value2: Int) {
// code
}
if else:
else跟if右边大括号之后, esle前后需加1个空格
if flag {
// code
} else {
// code
}
switch:
case跟switch左对齐
switch value {
case 1:
// code
case 2:
// code
default:break
}
不使用try!:
// 正确
let value = try? decodeData()
// 错误
let value = try! decodeData()
单行尽量不要超过120个字符:
函数体长度不超过100行:
代理协议:
需要使用extension来单独的实现
class MyClass: UIViewController {
}
// MARK: - UITableViewDataSource/Delegate
extension MyClass: UITableViewDataSource, UITableViewDelegate {
}
// MARK: - UserInfoDelegate
extension MyClass: UserInfoDelegate {
}
网络层:
需要使用extension来单独的实现, 并且放在类的最后面
// MARK: - NetWork
extension MyClass {
/// 加载数据1
private func loadData() {
// code
}
/// 加载数据2
private func loadOtherData() {
// code
}
}
MVC类的层次结构:
/// 我的主页
class MyClass: UIViewController {
// MARK: - Property
private lazy var titleBtn: UIButton = configure(.init()) {
$0.setTitle("标题按钮", for: .normal)
$0.addTarget(self, action: #selector(titleAction), for: .touchUpInside)
}
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Method
private func add1() {
// code
}
// MARK: - MethodPublic
func add() {
// code
}
// MARK: - Actions
/// 标题按钮点击
@objc private func titleAction() {
// code
}
}
// MARK: - UITableViewDataSource/Delegate
extension MyClass: UITableViewDataSource, UITableViewDelegate {
}
// MARK: - NetWork
extension MyClass {
/// 加载数据1
private func loadData() {
// code
}
/// 加载数据2
private func loadOtherData() {
// code
}
}