A language.

Use US(American) English, not UK(British) English or Chinese pinyin.

US: UIColor *myColor =[UIColor blueColor]; UK: UIColor *myColour =[UIColor blueColor]; UIColor *wodeYanSe =[UIColor blueColor];Copy the code

Two. Naming rules

1. Names of constants are preceded by a lowercase letter k. The rest follow the small hump nomenclature (the first word is all lowercase and the first letter of the next word is capitalized).

NSTimeInterval kAnimationDuration = 0.3;
Copy the code

2. Macros are named with a prefix of two uppercase letters followed by the big camel name.

#define KKScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define KKAppVersion @"appVersion"
Copy the code

3. Enumeration names follow objective-C internal framework definition. The enumeration content in Enum must be named starting with the Enum type name.

typedef NS_ENUM(NSInteger, FulowersMoveDestination)
{
    FulowersMoveDestinationTop,
    FulowersMoveDestinationBottom,
    FulowersMoveDestinationLeft,
    FulowersMoveDestinationRight,
};
Copy the code

4. The overall naming of the class adopts the big camel name (the first letter of each word is capitalized). Class prefix: capitalize the developer’s first letter. Class suffix: use the full name of the corresponding class.

NavigationController: LBYNavigationController ViewController: LBYHomeViewController TableViewController Table controller: LBYTableViewController TabBarController Label controller: LBYTabBarControllerCopy the code

5. Method naming When three or more method arguments are used, line breaks are aligned (colon alignment, colon before parameter variable, colon after parameter value). Method declaration:

+ (instancetype)initWithPersonName:(NSString *)name
                           withAge:(int)age
                           withSex:(NSString *)sex
                        withHeight:(float)height 
                        withWeight:(float)weight;
Copy the code

Method calls: Avoid colon alignment.

6. Attributes and objects are named in the way of modification + type,BOOL type is prefixed, words follow the small camel name. Declaration attributes, the brackets of the sequence is: nonatomic, readonly, strong.

@property (nonatomic, assign) BOOL isLogin;
@property (nonatomic, weak) UITextField *loginNameTextField;
@property (nonatomic, copy) NSString *studentClientName;
@property (nonatomic, weak) UILabel *loginTipLabel;
@property (nonatomic, weak) UIButton *loginButton;
Copy the code

3. Comments

Comments are used to explain and explain methods or variables. They can be named reasonably and clearly. 1. Public class method annotations declare class methods in a. H file, using documentation annotations, to specify the specific function of the method, the meaning of all parameters and returned parameter values.

@param name @param age @param sex @param height @param weight @return Returns the person object */ +  (instancetype)initWithPersonName:(NSString *)name withAge:(int)age withSex:(NSString *)sex withHeight:(float)height withWeight:(float)weight;Copy the code

2. Private object method annotation in the. M file to achieve the object method, using document annotation, to specify the specific role of the method, if there are parameters and return value, need to add the meaning of all parameters and returned parameter value.

/** create tableView UI */ - (void)setupTableViewUICopy the code

3. Comments for internal logical codes Complex logical codes are commented above the codes in the form of a double slash, a single space, and a specific comment

- (void)viewDidLoad { [super ViewDidLoad]; / / comment if (...). {... }}Copy the code

4. Attribute annotation

/ / @property (nonatomic, weak) UIButton *loginBtn;Copy the code

Group functions and use #pragma mark – to add tags to important logical code for easy reading

#pragma mark - Lifecycle
- (instancetype)init 
- (void)dealloc 
- (void)viewDidLoad 
- (void)viewWillAppear:(BOOL)animated 
- (void)didReceiveMemoryWarning 

#pragma mark - IBActions
- (IBAction)submitData:(id)sender 
    
#pragma mark - Public
- (void)publicMethod 
    
#pragma mark - Private
- (void)privateMethod 
    
#pragma mark - Custom Protocol
- (void)tabbarBottomView:(LBYTabbarBottomView *)tabbarBottomView didSelectIndex:(NSUInteger)index didSelectBtn:(BYBottomButton *)selectBtn

#pragma mark - UITextFieldDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
    
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

#pragma mark - NSObject
- (NSString *)description 
Copy the code

6. Printing about NSLog, when the final version of the project or SDK is completed, the printed comments need to be removed (the PCH precompiled file can be used to prohibit NSLog printing).

Iv. Formatting

1. Assign a space to the left and right of the = sign.

static const int count = 0; 
Copy the code

The +/- sign of a class method or object method method is separated by a space between the left curly brace. The curly brace is wrapped, and the upper and lower curly braces are aligned.

- (void)viewDidLoad 
{

}
Copy the code

2. Property declarations are separated by a space between the left parenthesis and the next property modifier, a space between the right parenthesis and the property type, and a space between the property type and the property variable. When you declare a string type, there is a space between the NSString and the number, and the number is connected to the property variable, and it should be NSString studentClientName, not NSString studentClientName, not NSString * studentClientName.

@property (nonatomic, assign, readwrite) BOOL isLogin;
@property (nonatomic, copy, readwrite) NSString *studentClientName;
Copy the code

3. In the for loop, there is a space between for and the left parentheses, a space between I and “<=”, a space between “<=” and “3”, followed by the seal number, and a space between the seal number and I. Braces wrap a line with a pair of braces aligned above and below.

for (int i = 0; i <= 3; I++) {// statement}Copy the code

4. Conditional statements Do not omit anything that requires braces. using

if (isLogin)
{   
    return success;
}
Copy the code

not

if (isLogin)
    return success;
Copy the code

Is not

if (isLogin) return success;
Copy the code

When a case statement contains more than one line of code, curly braces should be added. As shown in Case 2.

switch (condition) { case 1: // ... break; case 2: { // ... // Multi-line example using braces break; } case 3: // ... break; default: // ... break; } when used in the switch enumerated type, default is not needed. The switch (FulowersMoveDestination) {case FulowersMoveDestinationTop: / /... break; case FulowersMoveDestinationBottom: // ... break; case FulowersMoveDestinationLeft: // ... break; case FulowersMoveDestinationRight: // ... break; }Copy the code

5. The singleton

The shared+ class name is used as the method name of the singleton

@implementation LBYNetworkTool
+ (instancetype)sharedBYNetworkTool
{
    static LBYNetworkTool *instance;
    {    
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{        
            instance = [[LBYNetworkTool alloc]init];
        });
    }    
    return instance;
}
@end
Copy the code

6. The UI

When setting up the UI, use setup as the method name prefix and place the UI layout in the corresponding method

- (void)setupTableViewUI
- (void)setupNavigationUI
- (void)setupCollectionViewUI
Copy the code