Things you should pay attention to when writing OC code: Be familiar with OC

1.OC is the C language that adds object-oriented features. The check is done at runtime. The runtime environment is determined by the compiler.
2. Master the memory and pointers of C language.
3. Introduce as few other header files as possible into the header file of the class to reduce the number of header files introduced by users. Will increase compilation time.
4. When forward declaration cannot be used, such as declaring a certain protocol, move the protocol declaration to a category. If that doesn't work, you need to put the protocol in a separate header file and then reference it.
5. Use literal syntax more often and less equivalent methods. (Literal syntax: NSString *str = @"hello world"; NSNmuber *someNumbr = @1;). Benefits: Compilation time is reduced, and time to obtain data is also reduced.

(1) It is relatively simple to create arrays, dictionaries, etc.:

    NSArray *array = @[@"xiaomao", @"xiaogou", @"xiaoming"];
    NSLog(@"%@",array);
    
    NSDictionary *perDic = @{
        @"name" : @"张三",
        @"age" : @"19",
        @"meaaage" : @"好好学习,天天向上"
    };
    NSLog(@"%@",perDic);

(2) Arrays can also be accessed using literals:

    NSString *name = [perDic objectForKey:@"name"];
    
    NSString *nameOne = perDic[@"name"];
    NSLog(@"%@---%@",name,nameOne);
6. Use more type variables and less #define preprocessing instructions: (static const NSString * CString = @"nihao")

​ Constants visible only within mutation units.

7. Define global variables, constant variables visible to the outside world: (extern NSString *const ECString)

(1) Declared in the header file and defined in the implementation file.

(2) extern tells the compiler that there is an ECString constant in the full symbol table.

(3) The names of constants are generally prefixed with the class name, so as to avoid errors with the same name.

8. Use enumerations to represent status, options, and status codes (enum enumeration type)
typedef enum Econnection {
    EconnectionOne = 1,
    EconnectionTwo,
    EconnectionThree
}Econnection;

(1) Numbering starts from 0.

(2) Renaming can be done with typedef.

typedef enum Econnection Econnection;

(3) Do not add the default branch to the switch that handles enumeration types.

Guess you like

Origin blog.csdn.net/weixin_42357849/article/details/120496143