# # # 1. Understand the Objective – c

  1. OC is an object-oriented language
  • Message structures are used instead of function calls
    • Message structure: The code executed by the runtime is determined by the runtime environment
    • Function calls: The runtime code is determined by the compiler
  • The important work of the OC is done by the “run-time components,” not the compiler
  • OC is a superset of C, so everything in C is applicable when writing OC code
  • OC uses a dynamically bound message structure that checks the type of the object only at runtime, and the runtime environment, not the compiler, determines which code to execute at the end of a message

###2. Introduce as few other headers as possible in the class header file

  1. OC also uses header files and implementation files to separate code
  2. In header files, you can use the @class class name (i.e., “forward declaration”) if you only need to know that this is a class, but not the implementation of the class, etc. In the implementation file, we might need to know the class interface details, so we need to use #import class name in the implementation file
  • In this way, the number of imported header files can be reduced and the coupling between classes can be reduced as much as possible
  • When forward declarations are not possible, try to move the “this class follows a protocol” declaration into the “class-Continuation” category instead. You can also put the protocol in a separate header file and import it

####3. The difference between #import, #include, @class import: the keyword of OC type import header contains the entire content of the file

  • It will automatically import once
  • Does not cause cross-compilation, because using include causes cross-compilation when OC is C++ or C

Include: is the C/C++ import header keyword

  • There is no longer a way to import header files in OC

@class: simply declare a class, and do not include the implementation of the attributes in the class

  • Can solve the problem of loop dependence (A references B, B references A)

###4. Use more literal syntax and less its equivalent

  1. Use literal syntax to create strings, arrays, arrays, dictionaries. This will be more succinct than the normal way of creating such objects
  2. You should access array subscripts or key pairs in dictionaries by fetching subscripts
  3. When you create arrays and dictionaries with literals, if there’s nil in the value, it throws an exception, and you have to make sure there’s no nil in the value

###5. Use more type constants and less #define preprocessor

  • Do not define constants with preprocessor instructions. Constants defined in this way do not contain type information, and the compiler simply does a look-and-replace operation at compile time. If someone redefines a constant value, the compiler does not issue a warning, resulting in inconsistent values in the program
  • Use static const in the implementation file to define a constant that is visible only within the compilation unit. Since the constant is not in the global symbol table, it does not need to be prefixed with its name
  • Extren is used in the header file to declare global constants, and the value is defined in the relevant implementation file. Such constants will appear in the global symbol table, so its name should be distinguished, and the class name is generally added in front of it

###6. Use enumerations to represent states, options, status codes

  • Enumerations are a way of naming constants so that the written code is easier to read. The compiler assigns enumerations a unique number, starting at 0, incrementing each enumeration by one.
enum DMTConnectionState {

    DMTConnectionStateDisconnected,
    DMTConnectionStateConnecting,
    DMTConnectionStateConnected,

};

enum DMTConnectionState state = DMTConnectionStateConnected;

Copy the code
  • The above definition syntax is not very concise, we can use the typedef keyword to redefine, so that we do not need to write enum every time, we can use DMTConnectionState instead of enum DMTConnectionState
typedef enum DMTConnectionState DMTConnectionState;
DMTConnectionState state = DMTConnectionStateConnected;

Copy the code
  • C++11 standard: you can specify what underlying data type to use to hold variables of enumerated types. The advantage of this is that you can declare enumeration variables forward. If you do not specify the underlying data type, the compiler does not know the size of the underlying data type, so it is not clear how much space to allocate to the variable when using this enumerated type.
enum DMTConnectionState : NSInteger{
    DMTConnectionStateDisconnected,
    DMTConnectionStateConnecting,
    DMTConnectionStateConnected,
};
Copy the code

It is also possible to manually specify a value for an enumeration member instead of a compiler-assigned ordinal

/ / the code we manually to DMTConnectionStateDisconnected value is 1 0 instead of using the system allocation, In the next few enumerated increasing will be based on a 1. Enum DMTConnectionState {DMTConnectionStateConnecting DMTConnectionStateDisconnected = 1, DMTConnectionStateConnected, };Copy the code
  • When you define an enumeration, the options of the enumeration can be combined with each other. You can define the values of the enumeration as binary, which can be combined through the bitwise and or operators
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};

Copy the code
  • Some helper macros are defined in the Foundatin framework
typedef NS_ENUM(NSUInteger,DMTConnectionState){

    DMTConnectionStateDisconnected,
    DMTConnectionStateConnecting,
    DMTConnectionStateConnected,  
};

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
Copy the code