The purpose of,

In order to facilitate project maintenance and specification development, and promote the efficiency of code review among members, the following development specifications are proposed. If you have better suggestions, welcome to put forward.

Intended audience for this document: iOS developers.

2. Naming conventions

The naming of the code is strictly forbidden to use pinyin and English mixed way, let alone direct use of Chinese way. Correct English spelling and grammar make it easy for readers to understand and avoid ambiguity.

* Note: Even pure pinyin naming should be avoided. However, alibab, Taobao, Youku, Hangzhou and other international common names can be regarded as English.

The big Hump rule: Capitalize the first letter of each word. Example: NameTextField. The small Hump rule: Lower case the first letter of a word, uppercase the rest. Example: nameTextField.Copy the code

2.1 Project Naming

Project names follow the big hump name. For example: AoRiseProject

2.2 Bundle Identifier Naming

Bundle Identifier: Anti-domain name naming convention, all lowercase letters, domain name suffix + company top-level domain name + application name.

For example: com. Company name (Kangqiao). Service. Guanjia

2.3 the name of the class

Classes are named after the big hump. Prefix + function + type.

For example, KQ + Login + ViewController.

Common control class naming type mapping table (prefix: KQ in the following table. If it is not listed in the following table, remove the first letter of UI and follow the actual rules.)

| | control name type sample | | | -- - | -- - | -- - | | UIViewController | ViewController | KQBaseViewController | | UView | View |KQBaseView| | UITableView |TableView |KQOrderTableView| | UITableViewCell |Cell |KQOrderListCell| | UIButton |Button |KQSuccessButton| | UILabel |Label |KQSuccessLabel| | UIImageView |ImgView |KQGoodsImgView| | UITextField |TextField |KQNameTextField| | UITextView |TextView |KQSuggestTextView|Copy the code

Other class related comparison table

| | | function type sample | | -- - | -- - | -- - | | tools | Tool | KQOrderTool | | | management class Manager | KQOrderManager | | | Model class Model | KQOrderListModel | | | DataBase class DataBase, table name + DBHelper | KQFriendDataBase, KQUserTableDBHelper | | category | XXX + (range, such as the Extension, Additions or functions, such as Frame, Nib, Block) | KQUIButton + Additions, KQUIButton + Block |Copy the code

2.4 UIViewController should be classified as follows

 // External pass argument + @ibOutlet

// MARK: - life cycle

// MARK: - UI

// MARK: - events

// MARK: - private methods

// MARK: - UITableViewDataSource

// MARK: - UITableViewDelegate(The proxy order is down)// MARK: - network

// MARK: - getters & setters
Copy the code

* Note: All views or objects should be created with lazy loading and called with self.testButton.

import UIKit
class KQTestViewController: UIViewController {
    / / / user Id
    public var userId: String?
    / / / head
    @IBOutlet weak var headerImageView: UIImageView!
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        / / create the UI
        self.buildUI()
    }


    // MARK: - UI
    / / / to create the UI
    func buildUI() {
        / / add testButton
        self.view.addSubview(self.testButton)
        self.testButton.mas_makeConstraints { (make) inmake? .top.mas_equalTo()(0) make? .left.mas_equalTo()(0) make? .right.mas_equalTo()(0) make? .bottom.mas_equalTo()(0)}}// MARK: - events

    /// Click the test button

    /// -parameter sender: button

    @objc func testButtonClick(_ *sender: UIButton) {

    // Introduce yourself

    let _ = self.introduction("jack")}// MARK: - private methods

    /// Introduce yourself

    /// -parameter name: Parameter name

    /// - Returns: results
func introduction(_ name: String?). -> Bool {if (name ?? "").count > 0 {
print("my name is \(name!) !")
return true
} else {return false}}// MARK: - UITableViewDataSource
    // MARK: - UITableViewDelegate
// MARK: - network
// MARK: - getters & setters
private lazy var testButton: UIButton = {
let testButton = UIButton(type: .custom)
testButton.backgroundColor = .red
testButton.setTitle("Test".for: .normal)
testButton.addTarget(self, action: #selector(testButtonClick(_ *:)), for: .touchUpInside)*

return testButton

}()
}

Copy the code

2.5 Variables and Methods

Variables and methods are named after the small hump. Such as:

TestButton func testButtonClick(_ * Sender: UIButton) responds to the event.Copy the code

2.6 constant

Global constants: project prefix + all caps, separated by underscores For example:

let KQ_SCREEN_WIDTH
Copy the code

2.7 parameter name

Parameter names are named after small humps and are written in the style of apple’s native methods as much as possible. Make it as readable as possible, and see the method name to know what the method is used for. Such as:

func setData(_ *imageUrl: String? ,* _ name:String? , content:String?).Copy the code

Resource file specification

3.1 Naming resource Files

All lowercase, use the underscore naming method, prefix to distinguish. All resource files need to be prefixed with the project prefix (lowercase) naming mode: the suffix _smal for small graph, _big for large graph, logical names can be multiple words underlined, using the following rules:

- module name \ _ use logical name * _ * - module name \ _ use _ * * - use logical name * _ * - purpose _ * * | show | prefix (prefix engineering example KQ) sample | | | -- - | -- - | -- - | | button related | kq_btn_ | is as follows: Kq_home_btn_normal, kq_home_btn_red, kq_btn_normal, kq_btn_red_big | | background related | kq_bg_ | is as follows: kq_home_bg_header, kq_bg_main |Copy the code

3.2 Folder Naming

Create a folder It is best to create a physical folder, go to the project directory, create a folder and drag it into the project. The folders are named in the corresponding module hierarchy in English with a capital letter. Example: Model, View, Controller, Tool, Other, Service, etc.

Fourth, third-party library specifications

With the recent technology, the selection of open source library generally requires a relatively stable version. The project the author is maintaining should consider the author’s solution to the issue and the popularity of the developer. After selection, some encapsulation is necessary.

The project uses Cocoapods to manage open source third library files without manual import and manual addition of dependent libraries. If the third party does not support Cocoapods, you can manually import the project.

If the third party library needs to be modified, put it in the TheThirdLiabrary folder.

5. Annotation specification

To reduce the pain of others reading your code, make comments in key areas.

5.1 class annotation

 //

    // KQTestViewController.swift

    // KangYunYouJia

    //

    // Created by zhouxiang on 2020/12/1.

    // Copyright © 2020 [email protected]. All rights reserved

    //
Copy the code

   

This comment is automatically generated and can be set in Xcode. Created by computer username on Time when the file was Created. The name and email address behind Copyright 2020 are filled in and set by yourself. Specific Settings can be set in Xcode Project and Project Document. This will automatically add the header annotation each time a new class is created.

5.2 Method Notes

Method annotation, method external unified use option + command + /, method internal unified use // comment.

/// Introduce yourself

    /// -parameter name: Parameter name

    /// - Returns: results

    func introduction(_ name: String?). -> Bool {if (name ?? "").count > 0 {

            print("my name is \(name!) !")

            return true

        } else {

            return false}}Copy the code

   

5.3 Model Notes

Each property contained in a model must be commented with a corresponding comment, with /// annotations. Once the reader looks at the Model, it is clear what each field in the Model means and what it does.

    @interface KQUnitModel : NSObject

    /// Name of the community

    @property (strong, nonatomic, nullable) NSString *communityName;

    / / / area code

    @property (strong, nonatomic, nullable) NSString *communityCode;

    / / / area id

    @property (strong, nonatomic, nullable) NSString *cellId;

    /// Region name

    @property (strong, nonatomic, nullable) NSString *cellName;

    /// Floor list

    @property (strong, nonatomic, nullable) NSArray <KQFloorModel*> *floorList;

    @end

    

    

    class KQUnitModel: NSObject {
        /// Name of the community
        var communityName: String?
        / / / area code
        var communityCode: String?
        / / / area id
        var cellId: String?
        /// Region name
        var cellName: String?
        /// Floor list
        var floorList: [KQFloorModel]?
    }

Copy the code

* Note: If it is not a model attribute, it is another class attribute and needs to be commented, please follow the model attribute annotation method.

Six, coding specifications

  • Empty line between all methods.

  • Empty all code blocks with a line between them and remove any extra comments.

  • All custom methods need to be commented.

  • Try to use lazy loading (mentioned and required in the controller classification, other custom classes according to the controller format classification, no classification can be written.)

  • The ‘{‘ at the end of the code need not be a single line, including the method after, if, switch, etc.

  • The property must be defined in accordance with the following property, space a space after the parenthesis, write the class name, space a space followed by ** and the property name. For example: *

      / / / area

      @property (strong, nonatomic, nullable) NSString *communityName; 
Copy the code
  • Follow general code specifications and imitate apple apis.

  • Remove unused code.

  • If there are methods you never use, delete them (except for utility classes).

  • Methods that do not perform any business logic, please delete or give comments, remove excess resources or files, add necessary comments.

  • Large blocks of code need to be commented.

Vii. Other specifications

  • It is recommended that all items be combined with the navigation XIB. Setting the frame directly is not allowed. Pure storyboard development is not recommended.

  • Extract methods to remove duplicate code. It is also important to extract the necessary utility classes, which can be reused in future projects.

  • Use local variables whenever possible

  • Minimize double counting of variables.

  • Use singletons when appropriate. Using singletons can lighten load load, shorten load time and improve load efficiency. However, not all cases are applicable to singletons. Simply speaking, singletons are mainly applicable to the following three aspects:

– Controls the use of resources and controls the concurrent access of resources through thread synchronization.

– Controls the creation of instances to save resources.

– Controls the sharing of data, allowing multiple unrelated processes or threads to communicate without establishing direct correlation.

  • Finally, don’t forget to detect memory leaks.