OC is a superset of C, which adds object-oriented features to the C language. OC uses a dynamically bound message structure, that is, object types are checked at run time. It is up to the runtime environment, not the compiler, to decide what code to execute after receiving a message. In general, using forward declarations in the header files of one class to refer to other classes, and introducing headers of those classes in the implementation file, minimizes the coupling between classes before the class. More literal syntax and less literal syntax can reduce the length of source code and make it more readable

NSNumber *someNumber = [NSNumber numberWithInt:1];
Copy the code

Using literals makes code cleaner, with no extra syntax

NSNumber *someNumber = @1;Copy the code

Let’s first look at creating arrays without using literal syntax:

NSArray *array = [NSArray arrayWithObjects:@"object1"The @"object2"The @"object3"The @"object4", nil];
Copy the code

An array created using literal syntax

NSArray *array = @[@"object1"The @"object2"The @"object3"The @"object4"];
Copy the code

Not only is it simple, it’s also easy to manipulate arrays

[array objectAtIndex:1];Copy the code

Do not use preprocessing directives to define constants. The resulting constants contain no type information, which the compiler uses to perform lookup and replace operations before compilation. Even if someone redefines a constant value, the compiler will not generate a warning message, which will result in inconsistent constant values in the application. Use static const in the implementation file to define constants that are visible only within the compilation unit. Because such constants are not in the global symbol table, there is no need to prefix their names. Use extern in header files to declare global constants and define their values in the associated implementation files. Such constants appear in the global symbol table, so their names are delimited, usually prefixed by the name of the associated class. Enumerations should be used to represent the state of the state machine, option traces of state code equivalents passed to methods, and give these values easy-to-understand names. If the options passed to a method are represented as enumerations, and multiple options can be used at the same time, the value of each option is defined as a power of two so that it can be combined by bitwise or operation. Use the NS_ENUM and NS_OPTIONS macros to define enumeration types and indicate their underlying data types. Doing so ensures that the enumeration is implemented using the underlying data type of the developer’s choice, not the compiler’s choice. Do not implement the default branch in switch statements that handle enumerated types. This way, after adding a new enumeration, the compiler will tell the developer that the switch statement does not process all the enumerations.