1. What is the nature of OC? We usually write OC code, the underlying implementation is based on C\C++ code, so OC object-oriented is based on C\C++ data structure implementation.

2. How much memory does an NSObject take up? The system allocates 16 bytes to NSObject (obtained by malloc_size). In the ARM64 structure, an ISA pointer takes up only 8 bytes of memory. If an object has three attributes of type int and one of type int is 4 bytes, the memory used by the object isa multiple of the largest member variable (isa pointer), 24 bytes instead of 20 bytes, and the system allocates 32 bytes

3. What is the role of alloc method? During the initialization of class objects, the memory space needed by the object is calculated first, and then the memory space is opened in the heap

Alloc can be used to create multiple instance objects, such as an instance object, a class object, a meta-class object, and a class object. But the class object corresponding to multiple instance objects does have a unique copy of 2, Class objects store information in memory mainly including: ISA pointer,superclass pointer, property information (@property), object method information (instance method), protocol information (protocol), member variable information 3, Each class has only one meta-class object in memory. The meta-class object has the same memory structure as the class object, but its purpose is different. The information stored in memory includes isa pointer, superclass pointer, and class method information.

5. Where does the object’s ISA pointer point to? Instance isa refers to class, and when invoking an object method, it finds the implementation of the object method, and finally finds the implementation of the object method. When a class method is called, it finds the meta-class through the isa of the class and finally finds the implementation of the class method. The ISA of the meta-class points to the meta-class of the base class

6. What is the nature of KVO? Use the RuntimeAPI to dynamically generate a subclass, and let instance’s ISA point to the new subclass. When modifying an instance’s properties, the Fundation _NSSet*ValueAndNotify function is called. Will trigger the listener within the Observer’s monitoring method (observeValueForKeyPath: ofObject: change: context:), pay attention to change the member variables of an object directly, instead of calling set method, will not trigger the Observer method, KVO can be manually called by calling the willChangeValueForKey: and didChangeValueForKey: methods

7. What is the nature of KVC? When assigning a Value to an object using KVC, the following methods and attributes will be called in order to check setKey: Does the method exist? If it does, call it directly. If not, go to the next step to check whether the _setKey: method exists. If not, go to the next step to call member variables :_key, _isKey, key, isKey. When assigning a value to an attribute or member variable using KVO, KVO is triggered and the system automatically calls the willChangeValueForKey: and didChangeValueForKey: methods

The object methods and class methods in a Category are compiled as structures at the bottom, instead of being incorporated into the class objects and metaclass objects of Person. When you run the program, the Runtime merges the Category data into the class information (class object, metaclass object). What happens when the method name in the Category is the same as the method name defined by the class? At this point, the methods of the Category are placed at the front of the list of methods, and the original methods of the class are at the bottom of the list of methods. It’s not that the methods in the Category override the methods in the original class. If a class’s methods are called, they are queried backwards from the list of methods, whereas if the Category has the same methods, they are used directly

9. What is the difference between Category and Class Extension? When Class Extension is compiled, its data is already contained in the Class information and Category data is merged into the Class information at runtime

The load method is called when the runtime loads the class. The load method of the class is called before the load method of the Category. The load method of the parent class is called before the load method of the subclass is called. For unrelated classes, load is called in the order in which they were compiled. For categories, load is called only once. In most cases, the system does not call load automatically

When a class receives an alloc message for the first time, it calls its own +(void)initialize method. If it has a parent class, the parent’s +(void)initialize method will be called first

Load is automatically called from the address of the function. Initialize is called from objc_msgSend. Load is called once when the Runtime loads classes and classes. Initialize is called the first time a class receives a message. Each class is initialize only once (if the subclass does not implement initialize, the parent class’s initialize method is called, The initialize method of the parent class may be called several times.) The load of the parent class is called first, and the load of the class is called first, and the load of the subclass is called first. The initialize method of the parent class will be called by message mechanism. If the class does not have an initialize method, the initialize method of the parent class will be called. Therefore, the initialize method of the parent class will be called multiple times

The essence of block is to encapsulate the OC object of function call and function call environment. There are three main types of variables in OC, namely auto, static and global variables, among which auto and static modify local variables. In a block, if you use a local variable, the value of the variable will be captured by the block and changed after the block is called. The value of the variable in the block will not change. If a local variable is static, the block captures the address of the pointer to that variable, and the value used in the block changes accordingly

Block type MRC type A block that does not use an auto variable internally is an __NSGlobalBlock__ block that uses an auto variable internally, A block of type __NSStackBlock__ can be copied from stack to ARC by calling copy: Assigning a block of type __NSStackBlock__ to the __strong pointer copies the block to the heap as a method argument to the GCD API. Block in the heap block is in the heap when the Cocoa API Chinese method name contains a method argument called usingBlock, such as an array traversal method

15, Block memory analysis MRC type: The block in the stack does not retain the auto variable of the object type, only when the block is copied to the heap (reference count +1), When a block in the heap is released, the auto variable of the object type in it is released (reference count -1). If the auto variable of the object type is zero, the ARC type is released: When a block is copied to the heap, the __main_block_copy_0 function is called to strongly reference the auto variable of the captured object type, When a block is removed from the heap, the __main_block_dispose_0 function is called to dereference the auto variable of the captured object type. ARC provides the __weak keyword to modify the auto variable of the object type. When the auto variable of the captured object type is __weak, the __main_block_copy_0 method does not strongly reference the auto variable of the captured object type, even if the block is copied to the heap: In both ARC and MRC, the block in the stack does not strongly reference the captured object type auto (reference count +1), but only when it is copied to the heap. ARC objects of type auto, which are modified by __weak, are not strongly referenced when blocks are copied to the heap

The value of a static variable can be changed in a block because the underlying block captures the address of age, not the data stored in age. Block does not capture global variables, but directly used, so you can directly change the value, but ordinary local variables can not change the value, using __block can solve the block internal cannot modify the value of the auto variable, essentially through __block modified variables are wrapped into a pointer type

OC sends a message to the receiver using the objc_msgSend function. If the receiver is empty, the message is returned to the object msgsend function. When the message receiver has a value, the cache is looked at, and if the method has not been cached, the list of methods is queried

Dynamic method parsing and message forwarding in objc_msgSend, there are three stages: Message sending, dynamic method parsing, and message forwarding. When the method to be called is not found in the process of message sending, the dynamic method parsing will enter the phase of dynamic method parsing. When the dynamic method parsing does not find the method implementation to be called, the _objc_msgForward_impcache function will enter the phase of message forwarding. In the timer, NSProxy message forwarding is used to solve the problem of memory leakage that is not released at a fixed time. The latest timer block method does not cause strong references, but needs to use weak to solve the problem

As you can see from the actual code, these two member variables are passed in self and [Person class] respectively, so the receiver is self, and the method is searched from [Person class], so [super run]; The underlying code basically says: send self a message, the message name is run, the message starts from [Person class], super means that the query method starts from the parent class, not its own class object, the message receiver is self, not the parent class object, and the message sent is the method called

@dynamic tells the compiler not to generate setters and getters, and not to generate _age member variables, and to wait until runtime to add method implementations

As the name suggests, a Runloop is a loop that does something while the program is running. When no message occurs, the program stays asleep waiting for a message to occur. When a message is generated, the program processes the message and continues to sleep waiting. It just keeps running

22, multi-thread security risks when multiple threads access the same block of resources, it is easy to cause data confusion and data security problems to solve the multi-thread problem, the use of lock mechanism, such as spin lock, mutex lock, recursive lock

Atomic lock atomic lock is used to ensure atomicity operation of setter and getter properties. It is equivalent to adding thread synchronization lock inside getter and setter. Atomic lock can only ensure that the area inside setter and getter is safe, but cannot be guaranteed for external use