1. The origin of Object C
Object C is a language for message structures, and the code that the runtime should execute is determined by the runtime environment. The method to be executed is found at runtime, not at compile time. This is done by the Runtime Component, which contains all the data structures and functions needed to use Objective C’s object-oriented features. A runtime component is essentially a dynamic library that links to the code that the developer has written. The code glue together all the programs that the developer has written, so simply updating a runtime component can improve application performance.
Objects of Object C are allocated to the heap, and Pointers to objects are allocated to the stack (4 bytes for 32-bit systems, 8 bytes for 64-bit systems). Memory allocated to the heap must be managed directly, while memory allocated to the stack to hold variables is automatically cleaned up when its stack frame pops up.
Sometimes you encounter variables that do not contain * in their definition, and they may use “stack space”, such as the CGRect C structure.
Rule 2: Introduce as few other header files as possible in class header files
If you want to declare attributes of other classes in the header file, for example, you only need the type of the class and do not need to know all the implementation details of the class, you can simply use “forward declaration”, for example, to introduce @class SomeClass in the header file of the class. This can reduce the compilation time. However, if the class’s implementation file.m uses the latter, it needs to import the class’s header file using #import.
Protocol should be stored in a separate header file. For example, in a header file of a certain class, if other classes comply with this protocol, they need to import all contents of the class, resulting in mutual dependence and increasing compilation time.
Rule 3: Use literal syntax more often than equivalent syntax
//no
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"kitty"The @"name"The @"1353 * 94618"The @"number", nil];
NSString *str = [[NSString alloc] initWithString:@"hello kitty"];
//yes
NSNumber *intNumber = @10 ;
NSString *someString = @"hello";
NSArray *someArray =@[@"1"The @"2"];
NSDictionary *someDictionary = @{@"one" : @"1"The @"two" : @"2"};
Copy the code
The literals are concise and easy to read
Literals also have a weakness. They cannot express mutable types, but you can use copy directly on immutable objects, as follows: NSMutableDictionary *someMutableDictionary = [@{@"one" : @"1"The @"two" : @"2"} mutableCopy];
Copy the code
Use type constants more often than #define preprocessors
Use preprocessing instructions as follows:
#define ANIMATION_DURATION 0.3
Copy the code
The preprocessor directive does this by replacing all strings like ANIMATION_DURATION with 0.3 where it is referenced, so this is not recommended. It is much better to declare constants instead of macros.
// Declare a constant for NSTimeInterval /* static to indicate that it is visible in this compilation unit (in the.m file). If it is not declared, the compiler creates an "external symbol" for it. Const declares that the value of a variable cannot be modified. */ static const NSTimeInterval KAnimitionDuration = 0.3;Copy the code
Enumerations represent states, options, and status codes
/ / said the state of the network state enumeration typedef NS_ENUM (NSInteger NCNetworkStatus) {NCNetworkStatusConnected, / / the default is 0, Unless specified values NCNetworkStatusConnecting NCNetworkStatusdisConnected}; // NCNetworkStatus status = NCNetworkStatusdisConnected; switch (status) {case NCNetworkStatusConnected:
{
}
break;
case NCNetworkStatusConnecting:
{
}
break;
case NCNetworkStatusdisConnected:
{
}
break; Default: // Do not add options here, otherwise there will be a compilation prompt when we add options in the enumeration, in case we forget the logic associated with adding optionsbreak; } // Enumeration of options, Typedef NS_OPTIONS(NSUInteger, NCDirection) {NCDirectionNone = 0, NCDirectionUp = 1 << 0, NCDirectionDown = 1 << 1, NCDirectionLeft = 1 << 2, NCDirectionRight = 1 << 3 }; NCDirection direction = NCDirectionUp | NCDirectionLeft; // If the value is 5, it is a combination of up and leftCopy the code