What is the isa pointer to the objc object? What does it do?

Points to the object of his class so that methods can be found on the object

Best idea: The following diagram illustrates the relationships between objects, classes, and metaclasses:

The solid line in the figure isa pointer to super_class and the dotted line isa pointer to isa.

1. Root class(Class) is actually NSObject, and NSObject has no superclass, so the superclass of Root class(class) points to nil.

2. Each Class has an ISA pointer to a unique Meta Class

3. The superclass of Root class(Meta) points to Root class(class), that is, NSObject, forming a loop.

4. The ISA pointer for each Meta class points to the Root class (Meta).

How much memory does an NSObject take up?

Due to the memory allocation mechanism, each NSObject is allocated 16 bytes of memory.

But in fact only 8 bytes are used under 64 bits; Under 32 bits, only 4 bytes are used

The size of a member variable of an NSObject instance object is actually 8 bytes

Nature is

Gets the size of memory to which the obj-c pointer points, which is actually 16 bytes

Objects are memory aligned when they allocate memory, so in iOS, they allocate memory in multiples of 16 bytes. Can through the following url: openSource.apple.com/tarballs to view the source code.

Class_rw_t class_rw_t

Rw stands for read and write.

The attributes, methods, and protocols of ObjC classes are stored in class_rw_t:

Class_ro_t class_ro_t

Stores the properties, methods, and protocols that the current class follows as determined at compile time.

Five, to explain the understanding of the ISA pointer

The way toisa Pointer understanding, object ofisa Where does the pointer point?isa What are the two types of Pointers?

Isa is equivalent to is kind of

· The instance object ISA points to the class object

· Class objects refer to ISA metaclass objects

· Isa of a metaclass object points to the base class of the metaclass

There are two types of ISA

· A pure pointer to a memory address

· NON_POINTER_ISA, besides the memory address, has some other information

##isa source analysis

Check the Runtime source code to see that isa_t is shared. The simplified structure is as follows:

What about the Runtime method cache? The form of storage, the data structure, and the process of finding?

Cache_t Incrementally extended hash table structure. The bucket_t stored inside the hash table.

Bucket_t stores SEL and IMP key-value pairs.

For an ordered list of methods, use binary search

If it is an unordered list of methods, the search is traversed directly

Cache_t structure

Hash table lookup procedure, in the objc-cache.mm file

Above is the query hash function where cache_hash(k, m) is a static inline method that performs & on the passed key and mask and returns uint32_t index values. Do-while loop lookup, where the cache_next method subtracts the index by 1 when a conflict occurs.

Do we need to release the main object dealloc when we use the Runtime Associate method?

Not required under MRC or ARC, the associated objects are disposed much later in their life cycle than the objects themselves, and they are disposed in the object_Dispose () method called by Nsobjects-dealloc.

A:

The data structure of the instance object?

See the Runtime source code, lines 127-232 in objc-privately. h.

Essentially, objc_Object’s private property has only one ISA pointer. The memory address that points to the class object.

What is Method Swizzling?

In a nutshell, it’s a method exchange

When you call a method in Objective-C, you’re actually sending a message to an object, and the only way to find the message is by the name of the selector. Using the dynamic nature of Objective-C, you can sneak the method implementation of the corresponding selector at run time, so as to hook the method. So every class has a list of methods, and it’s a mapping between the name of the Method and the implementation of the Method. So the essence of a selector is actually a Method name, and an IMP is kind of like a function pointer, pointing to a specific Method implementation, and you can find the IMP by the selector.

Different methods of implementation

· Swap implementations of two methods with method_exchangeImplementations

· Implementation of replacement methods using class_replaceMethod

· Use method_setImplementation to directly set the IMP of a method

When does an unrecognized Selector exception occur?

When objc sends a message to an object, the Runtime library finds the actual class of the object based on the object’s ISA pointer, and then looks for a method to run in the list of methods in that class and the list of methods in its parent class. If the method is still not found in the topmost parent class, the message is forwarded. If the three-way message forwarding process is still not implemented, the program will die at runtime and throw an exception unrecognized selector sent to XXX.

How to add attributes to a Category? In what form are associated objects stored?

View the knowledge point of the associated object. Describe the associated objects in detail.

The associated objects are stored in a global singleton in the format of a hash table.

Can I add instance variables to the compiled class? Can you add instance variables to classes created at run time? Why is that?

You cannot add an instance variable to a compiled class. The ability to add instance variables to classes created at runtime;

1. Since the compiled class is already registered in Runtime, the object c_iVAR_list and instance_size instance variables in the class structure are determined. Runtime also calls class_setVARLayout or class_setWeaklvarLayout to handle strong weak references. So you cannot add an instance variable to an existing class.

2. The class created at run time can add instance variables and call class_addIvar function. But after calling objc_allocateClassPair and before objc_registerClassPair, for the same reason.

Data structure of class objects?

See the Runtime source code for details. The class object is objc_class.

It is relatively rich in structure. Inherits from the objc_object structure, so contains the ISA pointer

· ISA: Points to metaclasses

· superClass: Refers to the superClass

· Cache: Cache list of methods

· Data: As the name suggests, data. Is a encapsulated class_rw_t.

How does Runtime find the IMP address by selector?

Every class object has a list of methods, and in the list of methods is the name of the method, the implementation of the method, and the type of the arguments, and the selector is essentially the name of the method, and you can use that method name to find the corresponding method implementation in the list of methods.

How can Runtime set weak to nil? Do you know SideTable?

Runtime lays out the registered classes and places weak objects in a hash table. If the memory address of the weak object is a, then the table is searched for all weak objects with a key. If the memory address of the weak object is a, then the table is searched for all weak objects with a key. So set it to nil.

## A more detailed answer:

1. Initialization: Runtime calls objc_initWeak to initialize a new weak pointer to the address of the object.

2. When adding a reference: objc_initWeak calls objc_storeWeak(). Objc_storeWeak () is used to update the pointer to create the corresponding weak reference table.

3. When released, the clearDeallocating function is called. The clearDeallocating function first gets an array of all weak pointer addresses based on the object’s address, then walks through the array setting the data in it to nil, finally removing the entry from the Weak table, and finally cleaning up the object’s record.

The SideTable structure is responsible for managing the class’s reference count table and weak table,

Best idea: Read from the book advanced Programming in Objective-C

Initialization: Runtime calls objc_initWeak and initializes a new weak pointer to the address of the object.

When we initialize a weak variable, Runtime calls objc_initWeak in nsobject. mm.

Initialize “variable with weak modifier (obj1)” through objc_initWeak, passing at the end of the variable scope

Objc_destoryWeak frees the variable (obj1).

2. When adding a reference: objc_initWeak calls objc_storeWeak(). Objc_storeWeak () is used to update the pointer to create the corresponding weak reference table.

Objc_initWeak initializes “variable with weak (obj1)” to nil, and calls objc_storeWeak with “assignment object” (obj) as an argument.

That is to say,

Weak-modified Pointers default to nil (it’s safe to send messages to nil in Objective-C) and then obj_destroyWeak takes 0 (nil) as an argument and calls objc_storeWeak.

The previous source code is the same as the following.

The objc_storeWeak function registers the memory address of the second argument’s assignment object (obj) as its key and the memory address of the first argument’s weak-modified property variable (obj1) in the Weak table. If the second argument (obj) is 0 (nil), then the address of the variable (obj1) is removed from the weak table.

Because an object can be assigned to more than one variable with the weak modifier, you can register the addresses of multiple variables for a single key.

You can think of objc_storeWeak(&a, b) as objc_storeWeak(value, key), and set value to nil when key changes to nil. When B is non-nil, a and b point to the same memory address, and when B becomes nil, a becomes nil. Sending messages to A at this point does not crash: sending messages to nil in Objective-C is safe.

3. When released, the clearDeallocating function is called. The clearDeallocating function first gets an array of all weak pointer addresses based on the object’s address, then walks through the array setting the data in it to nil, finally removing the entry from the Weak table, and finally cleaning up the object’s record.

How do we handle weak Pointers when the object to which the weak reference refers is released? When releasing an object, the basic process is as follows:

1. Call objc_release

2. Because the object’s reference count is 0, dealloc is executed

3. In dealloc, _objc_rootDealloc function is called

4. The object_Dispose function is called in _objc_rootDealloc

5. Call objc_destructInstance

6. Finally, call objc_clear_deallocating

The objc_clear_deallocating function is called when the object is released:

1. Obtain a record of discarded objects whose addresses are key values from the Weak table

2. Set the addresses of all variables with weak modifiers contained in the record to nil

3. Delete the record from the Weak table

4. Delete the record where the address of the discarded object is the key value from the reference count table

Conclusion:

The Weak table is a hash table where Key is the address of the Weak object and Value is an array of addresses of the Weak pointer (whose Value is the address of the Weak pointer).

16,objcTo anilWhat happens when an object sends a message?

If you send a message to a nil object, the first thing you do is look for the isa pointer to the object and it returns at zero, so you don’t get any errors. It doesn’t collapse.

A:

If a method returns an object, then a message sent to nil will return 0(nil);

If a method returns a value of pointer type whose size is less than or equal to sizeof(void*), float, double, long double, or long long integer scaler, a message sent to nil will return 0;

If the method returns a structure, a message sent to nil will return 0. Each field in the structure will have a value of 0;

The return value of a message sent to nil will be undefined if the return value of the method is not one of the cases mentioned above.

What happens when Objc sends a message to an object?

When objc sends a message to an object, Runtime finds the actual class that the object belongs to based on the object’s ISA pointer, and then looks for a method to run in the list of methods in that class and the list of methods in its parent class. If it doesn’t find a method by the root class, it moves to intercept the call and forwards the message. Once found, To execute its implementation IMP.

IsKindOfClass and isMemberOfClass

What does the following code output?

Answer: 1000

A:

In isKindOfClass, we have a loop that determines whether class is equal to meta class, and then we continue to loop to determine whether class is equal to super class of Meta class, and then we continue to loop to super class, and so on.

NSObject class, and then we call isKindOfClass, and the first judgment is whether the meta class of NSObject and NSObject are equal, and we put a very detailed picture of that when we talked about the Meta class, We can also see from the diagram that the Metaclass of NSObject is not the same as itself. Then the second loop determines whether NSObject is equal to the superclass of the Meta class. The superclass of Root Class (Meta) is Root class(Class), which is NSObject itself. So the second loop is equal, so the output of the first line res1 should be YES.

For the first loop, the Meta class of Sark is different from the Meta class of [Sark]. For the second loop, The super Class of the Sark Meta Class refers to the NSObject Meta Class, which is not equal to the Sark Class. The third for loop, the super Class of the NSObject Meta Class refers to the NSObject Class, which is not equal to the Sark Class. On the fourth loop, the super Class of NSObject Class points to nil, which is not equal to the Sark Class. After the fourth loop, exit the loop, so the third line res3 prints NO.

IsMemberOfClass source code implementation is to get its own ISA pointer and compare to itself, whether equal.

The second line isa points to the Meta Class of NSObject, so it’s not equal to the NSObject Class. In the fourth line, isa points to the Meta Class of Sark, which is also different from the Sark Class, so the second line res2 and the fourth line res4 both print NO.

When are categories merged with existing classes after compilation?

1. After the program is started and compiled, Runtime is initialized by calling _objc_init.

2. Then go to map_images.

3. Next, call map_images_nolock.

4. Then there is read_images, which reads all the information about the class.

5. Finally, call reMethodizeClass:, which means reMethodizeClass.

6. AttachCategories: is called inside the reMethodizeClass: method, which passes in Class and Category, merging the method list, protocol list, and so on with the original Class. Finally, it is added to the class_rw_t structure.

What are the uses of “Category”?

· Add methods and attributes to system classes (with associated objects).

· A large number of methods of a class can be classified according to different names.

21, Category implementation principle?

Is added to the corresponding structure for class_rw_t.

Category is actually a Category_t structure, and at runtime, any new methods that are added are inserted in reverse order to the top of the original method list, so different categories add the same method, and actually execute the last one.

Take a list of methods, which is actually a two-dimensional array.

Category, if you look at the source code, is actually a _catrgory_t structure.

For example, if we write a class of Nsobject+Tools in our program, then when compiled to C++, it will actually be:

The Category is separate from the original class when it’s just compiled, only after the program is running, through Runtime,

The Category and the original class are merged together.

Mememove, memcpy: this method is both displacement and copied, moved to the method of simple understanding is original, is just a new controls, to classify the position of the front left, then the classification of the methods, according to the reverse order insert, could conclude that it is, the longer participate in the classification of compilation, the inside of the method is effective.

22. What does the _objc_msgForward function do, and what happens if you call it directly?

_objc_msgForward is IMP type for message forwarding: when a message is sent to an object, but it is not implemented,

_objc_msgForward will try to do message forwarding.

_objc_msgForward uses the following methods to forward messages:

1. The List itemresolveInstanceMethod: method (or resolveClassMethod:).

2. The List itemforwardingTargetForSelector: method

3. List itemmethodSignatureForSelector: method

List itemforwardInvocation: Method

5. List itemdoesNotRecognizeSelector: square method

See Runtime at Work. Chapter 3: Runtime method invocation process;

[super class] [self class]

What does the following code output?

NSStringFromClass([self class]) = Son NSStringFromClass([super class]) = Son

Best answer: Self and super are understood in Objective-C.

Self is a hidden argument to the class that points to the instance of the class in which the method is being called;

Super is essentially a compiler identifier that points to the same message receiver as self. The difference is that super tells the compiler to call a method from the parent class, not from this class, when calling a method.

When calling a method with self, it starts from the list of methods in the current class. If not, it starts from the superclass. When using super, start from the list of methods in the superclass. Then call this method on the superclass.

When calling [super class], Runtime will call objc_msgSendSuper instead of objc_msgSend;

In the objc_msgSendSuper method, the first argument is an objc_super structure that contains two variables: receiver, which receives messages, and super_class, which is the parent class of the current class.

Objc_msgSendSuper should work like this:

Start with the list of methods that the objc_super structure points to, and call the selector in the superClass with objc-> Receiver. Note that the last caller is objc-> Receiver, not super_class!

So objc_msgSendSuper turns out to be:

Because the IMP of the class method in the superclass NSObject was found, and because of the incoming argument objc_super->receiver= self. Self is son, so it calls class, so when the superclass method class executes IMP, the output is still son, and then the output is the same, so it’s both son.