The preface

Short step without thousands of miles, not small streams into rivers and seas. I am an iOS development experience sharing porter who loves learning. Today I will tell you, I from the big factory where the interviewer carried to engage in iOS must ask Runtime interview questions. No more nonsense, go straight to the dry goods, remember to click on a like.

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

Points to his class object so that you can find methods on that object

Best Answer: The following diagram illustrates the relationship between objects, classes, and metaclasses:

1.Root class(class) is really 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), which is NSObject, forming a loop.

4. Isa pointer to each Meta class points to Root class (Meta)

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

But in fact, in 64-bit, only 8 bytes are used;

In 32 bits, only 4 bytes were used

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

Nature is

Gets the size of the memory pointed to by the Obj -c pointer, which is actually

Object memory is aligned when allocating memory, so in iOS, memory is allocated in multiples of 16 bytes.

Can through the following url: openSource.apple.com/tarballs to view the source code.

What do you think of class_rw_t?

Rw stands for readable and writable. The attributes, methods, and protocols of ObjC classes are stored in class_rw_t:

What is your understanding of class_ro_t?

Stores properties, methods, and protocols that were identified at compile time for the current class

Where is the isa pointer to an object? What are the two types of ISA Pointers?

Isa is equivalent to is kind of

  • The instance object ISA points to the class object
  • Class objects refer to isa metaclass objects
  • The 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, which holds some other information besides the memory address

Isa source code analysis

Sa_t is common in Runtime source code. The simplified structure is as follows:

Untime method cache The form of storage, data structure, and search process?

The hash table structure for cache_t incremental extensions. The bucket_t stored internally in the hash table.

Bucket_t stores SEL and IMP key-value pairs.

  • For an ordered list of methods, use binary lookup
  • If it’s an unordered list of methods, just walk through the search

Cache_t structure

Do I need to dealloc the main object with runtime Associate?

This is not required under EITHER MRC or ARC. The associated objects are released much later in their life cycle than the objects themselves

Released from object_Dispose () method called by nsobject-dealloc.

When is an exception called by an unrecognized selector?

Objc When sending a message to an object, the Runtime library finds the class that the object actually belongs to based on the object’s ISA pointer, and then locates it in that class

If no corresponding method can be found in the topmost parent class

In the message forwarding phase, if the message forwarding process for three times is not realized, the program will hang up and throw an unrecognized exception when running

Selector sent to XXX.

How do I add attributes to categories? In what form are associated objects stored?

View the knowledge point of the associated object.

Talk more about the association object.

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

Can I add instance variables to the compiled class? Can I add instance variables to classes created at run time?

Why is that?

You cannot add instance variables to compiled classes.

Ability to add instance variables to classes created at run time

1. Because the compiled class is registered with the Runtime, the list of objc_iVAR_list instance variables and instance_size are in the class structure

The memory size of the instance variable is determined, and the Runtime calls class_setVARLayout or class_setWeaklvarLayout

Handle strong weak references. You cannot add instance variables to existing classes.

2. Create classes at runtime that can add instance variables and call class_addIvar. But after calling objc_allocateClassPair, before objc_registerClassPair, for the same reason.

How to find the corresponding IMP address by selector runtime?

Each class object is a method list, method list records the name of the method, method implementation, and parameter type, in fact, selector is essentially the method name, through the method name can be found in the method list corresponding method implementation.

How does runtime automatically set weak variables to nil? You know SideTable?

The Runtime lays out the registered classes and puts weak objects into a hash table. If the reference count of this object is 0, the dealloc is generated. If the memory address of the weak object is A, the dealloc is generated. If the memory address of the weak object is A, the dealloc is generated. Set to nil.

A more detailed answer:

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

2. When adding a reference: the objc_initWeak function will call objc_storeWeak(), which updates the pointer pointer and creates the corresponding weak reference table.

3. When releasing, call the clearDeallocating function. The clearDeallocating function first fetches an array of weak pointer addresses based on the object’s address, then iterates through the array to set it to nil, deletes the entry from the Weak table, and clears the object’s record.

The SideTable structure is responsible for managing the reference count table and weak table of the class.

Best explanation: See Advanced Programming in Objective-C

3. When releasing, call the clearDeallocating function. The clearDeallocating function first fetches an array of weak pointer addresses based on the object’s address, then iterates through the array to set it to nil, deletes the entry from the Weak table, and clears the object’s record.

What happens to the weak pointer when the object to which the weak reference points is released? When an object is released, the basic flow is as follows:

1. Call objc_release

2. Execute dealloc because the reference count of the object is 0

3. In dealloc, the _objc_rootDealloc function is invoked

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 that is called when the object is released:

1. Obtain the address of the abandoned object as the key value from the weak table

2. Assign nil3 to all addresses containing the weak modifier variable in the record. Delete the record from the weak table

4. Delete the records of discarded objects whose addresses are keys 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 the address of the Weak pointer.

What happens when objC sends a message to nil?

If you send a message to a nil object, the first thing you do when you look for the isa pointer to the object is that the 0 address is returned, so you don’t get any errors. It doesn’t collapse. A:

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

If the method returns a pointer, the sizeof the pointer is less than or equal to sizeof(void*), float, double, long double, or

Integer scalar of long long, message sent to nil will return 0;

If the method returns a struct, the message sent to nil returns 0. The value of each field in the structure will be 0;

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

What happens when objC sends a message to an object?

When objC sends a message to an object, the Runtime will find the class to which the object belongs according to the isa pointer of the object, and then run the method through the list of methods in the class and the list of methods of its parent class. If the root class is not found, the runtime will intercept the call and use the message forwarding mechanism. Go ahead and execute its implementation IMP.

A:

In isKindOfClass, there is a loop that evaluates whether the class is equal to the super class of the meta class, and then evaluates whether the class is equal to the super class of the meta class, and so on.

[NSObject Class] after it’s done, I call isKindOfClass, and the first time I check is if the meta class of NSObject and NSObject are equal, and I showed you a very detailed graph when I talked about meta class, We can also see from the diagram that NSObject’s meta Class is not the same as itself. Then the second loop checks whether NSObject is equal to the superclass of the Meta Class. The superclass of Root class(meta) is Root

Class, which is NSObject itself. So the second loop is equal, so the first line res1 output should be YES.

Similarly, isKindOfClass is called after the execution of [Sark class]. The first for loop, Sark’s Meta class is different from that of [Sark class]. The second for 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 NSObject Meta Class points to NSObject Class, which is not equal to Sark Class. The fourth loop, the super Class of NSObject Class points to nil, not equal to Sark Class. After the fourth loop, the loop exits, so the third line res3 outputs NO.

IsMemberOfClass gets its own isa pointer and compares it to see if it is equal.

The second line isa refers to the Meta Class of NSObject, so it is not equal to the NSObject Class. In line 4, isa refers to Sark’s Meta Class, and Sark Class is not equal, so res2 and res4 both print NO.

19. When is a Category merged with the original class after compilation?

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

  2. And then we have map_images.

  3. Next call map_images_NOLock.

  4. Then there’s read_images, which reads the information about all the classes.

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

  6. Within the reMethodizeClass: method is called attachCategories:, which passes in classes and categories and merges the list of methods, protocols, and so on with the original Class. And then we add it to the class_rw_T structure.

What are categories used for?

 Add methods and attributes (you need to associate objects) to the system class.

 There are a lot of methods in a class that can be grouped under different names.

How does Category work?

Is added to the corresponding structure of class_rw_t.

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

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

And if you look at the source code, you’ll see that Category is actually a _catrgory_t structure.

For example, if we write a Nsobject+Tools class in our program, then when compiled into C++, it actually reads:

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

_objc_msgForward is an IMP type for message forwarding: when a message is sent to an object and it is not implemented, _objc_msgForward will attempt to forward the message.

A: _objc_msgForward involves the following methods in the process of message forwarding:

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

  2. The List itemforwardingTargetForSelector: method

  3. The List itemmethodSignatureForSelector: method

  4. The List itemforwardInvocation: method

  5. The List itemdoesNotRecognizeSelector: method

For details, see: Application of Runtime in Work chapter 3 Runtime method Call Process;

conclusion

Xiaobian is not easy, excellent people have already liked. As a wandering in the major platforms of senior porters, collected a lot of little video information, iOS factory interview information, free public classroom links, if you just need, just I just have, to click to receive information, free to share.