Effective ObjectiveC 2.0 Reading Notes Chapter 1: Familiarize yourself with ObjectiveC

Objective-C adds object-oriented features to the C language through a new set of syntax. Square brackets are frequently used in Objective-C syntax, and they are not shy about writing extremely long method names.

Item 1: Understand the origins of the Objective-C language

All Objective-C objects must be declared like this: NSString *someString = @"the string";, because the memory occupied by the object is always allocated in the "heap space" and never in the "stack". Objective-C objects cannot be allocated on the stack.

If another variable is declared to point to the same NSString object at this time, that is NSString *anotherString = someString;, both variables are of NSString *type, which means that two pieces of memory are allocated in the current "stack frame", and the size of each piece of memory can accommodate A pointer (4 bytes on a 32-bit computer, 8 bytes on a 64-bit computer). The values ​​in these two pieces of memory are the same, which is NSStringthe memory address of the instance.

Memory allocated on the heap must be managed directly by the developer, while memory allocated on the stack to hold variables is automatically cleaned up when its stack frame is popped.

Objective-C abstracts heap memory management. There is no need mallocto freeallocate or free the memory occupied by the object. The Objective-C runtime system abstracts this part of the work into a memory management framework called "reference counting".

In Objective-C code, you sometimes encounter variables that do not contain * in their definitions, and they may use "stack space " . What these variables hold are not Objective-C objects. As in the CoreGraphics framework, CGRectthis structure is used by the entire system framework, because performance will be affected if Objective-C objects are used instead. Compared with creating a structure, creating an object also requires additional overhead, such as allocating and freeing heap memory. If you only need to store non-object types such as int, float, double, etc., then this structure is usually fine.charCGRect

This paragraph better illustrates the difference between heap and stack in Objective C. Objects are stored in the heap, which need to be managed by developers, and pointers are placed in the stack, pointing to these objects.

 

Item 2: Introduce as few other header files as possible in the class header file

Every time you include other header files in a header file, ask yourself whether it is really necessary to do so. If you can replace the import with a "forward declaration", then don't import it. The so-called forward declaration is the declaration that starts with @class . If a header file must be imported to implement properties, instance variables, or to conform to the protocol, move it to the "class-continuation category" as much as possible. Doing so not only reduces compilation time, but also reduces dependencies on each other.

 

Item 3: Use literal syntax more than its equivalent

Key Points:

Strings, numbers, arrays, dictionaries should be created using literal syntax. This is more concise than the normal way of creating such objects. For example NSNumber *someNumber = @12; NSArray *anArray = @[@12, @22, @1.2];

NSDictionay *aDic = @{@"firstName":@"Johnny",@"lastName":@"Sheng"}

Array subscripts, or elements corresponding to keys in a dictionary, should be accessed by taking the subscript operation. anArray[1] and aDic[@"firstName"]

When creating an array or dictionary with literal syntax nil , an exception is thrown if there are values. Therefore, it is important to ensure that the value does not contain nil。

Item 4: Use more type constants and less #define preprocessor directives

Example:

// 定义“只在编译单元内可见的常量”
// In the implementation file
// 局部常量:用static修饰后,不能提供外界访问
static const NSTimeInterval kAnimationDuration = 0.3; // 变量不可修改
// 根据const修饰的位置设定能否修改
static const NSString *kStringConstant1 = @"VALUE1"; // 变量可以被修改
static NSString *const kStringConstant2 = @"VALUE2"; // 变量不可修改

// 声明全局变量
// In the header file
extern NSString *const EOCStringConstant;
// 定义全局变量
// In the implementation file
NSString *const EOCStringConstant = @"VALUE";

const modification position is different, what does it mean?

  1. const NSString *HSCoder = @"HSCoder";
    "*HSCoder" cannot be modified, "HSCoder" can be modified

  2. NSString const *HSCoder = @"HSCoder";
    "*HSCoder" cannot be modified, "HSCoder" can be modified

  3. NSString * const HSCoder = @"HSCoder";
    "HSCoder" cannot be modified, "*HSCoder" can be modified

Note: 1 and 2 are not really different.

Conclusion: The total on the right side of const cannot be modified.

Important:
Do not define constants with preprocessing directives, because constants defined in this way have no type information, and the compiler will just perform a find and replace operation based on it before compiling. Even if someone redefines the constant value, the compiler will not generate a warning message, which will lead to inconsistent constant values ​​in the application.

Used in implementation files static constto define "translation-uinit-specific constants". Since such constants are not in the global symbol table, there is no need to prefix their names.

Use in header files extern to declare global variables and define their values ​​in the associated implementation files. Such constants appear in the global symbol table, so their names should be distinguished, usually prefixed with the class name associated with them.

 

Item 5: Use enumerations to represent states, options, and status codes

Key Points:

Use NS_ENUMand NS_OPTIONSmacros to define enumeration types and specify their underlying data types. Doing this ensures that the enumeration is implemented with the underlying data type chosen by the developer, not the type chosen by the compiler.

In switch statements , do not implement default branch. In this case, after adding a new enumeration, the compiler will alert the developer that the switch statement does not handle all enumerations.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058497&siteId=291194637