More:

Interview questions for iOS (part 1)

Interview questions for iOS (Part 2)

Want to enter big factory, interview question essential! (iOS Interview collection!)

IOS Interview Materials

1. How does Category work?

  • 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.
  • The Category is separate from the original class when it’s just compiled, and it’s only merged with the original class after the program is running, through Runtime.

Where does the object’s ISA pointer point to? What are the two types of ISA Pointers?

  • Isa is equivalent to is kind of

    Isa for instance objects points to class objects

    Isa for class objects points to 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, besides the memory address, has some other information

3. How does Objective-C implement multiple inheritance?

Object-c classes do not support multiple inheritance, but only single inheritance. If you want to achieve multiple inheritance, you can use the following methods to achieve it indirectly

  • By combination

    A and B are combined as components of class C

  • Implemented by protocol

    Class C implements the protocol methods of classes A and B

  • Message forwarding implementation

    Method of forwardInvocation:

4. How to implement the Weak attribute in Runtime?

Weak Indicates that the property defines a nonowning relationship. When a new value is set for such a property, the setting method neither holds the new value (the newly pointed object) nor releases the old value (the object to which it was pointed).

Runtime lays out the memory of registered classes. In a coarser-grained sense, there is a hash table, which is a global table with the memory addresses of weak objects as keys and all the weak Pointers to that object as values. When the reference count of this object is zero, dealloc will be set to nil. If the memory address of this object is a, it will search the weak table for all weak objects with key a and set it to nil.

The runtime implementation of weak is divided into three steps:

  • 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(), and objc_storeWeak() is used to update the pointer to something else. In this case, you need to unbind the weak pointer to the old object, Weak_unregister_no_lock) is called. If the new object pointed to by the pointer is not empty, a corresponding weak reference table is created. Weak_register_no_lock is called when the weak pointer is bound to the new object. During this process, there are some lock operations to prevent contention in multiple threads.
  • 3. Release: Calling clearDeallocating, clearDeallocating first gets an array of all the weak pointer addresses based on the object’s address, then walks through the array setting the data in it to nil, and finally deletes the entry from the Weak table. Finally, the record of the object is cleared.

5. Talk about the message mechanism of OC

  • The method call in OC is actually a call to the objc_msgSend function, sending a message to the receiver (method caller).
  • Objc_msgSend has three phases: message sending (current class, search in parent class), dynamic method parsing, and message forwarding

6. Runtime application

  • Add attributes to a category using association objects
  • Iterating over all member variables of the class (changing placeholder text color of Textfield, dictionary transfer model, automatic archive unfile)
  • Switching method implementation (method of switching systems)
  • Use the message forwarding mechanism to solve the exception problem that cannot be found by the method
  • KVC dictionary turn model

7. How does Runtime find the IMP address by selector?

Each class object has a list of object methods (object method cache)

  • The list of class methods is stored in a metaclass object (the class method cache) pointed to by the ISA pointer in the class object.
  • Each method structure in the list of methods records the name of the method, the implementation of the method, and the type of the arguments, but the selector is essentially the name of the method, and that’s the name of the method that you can use to find the corresponding method implementation in the list of methods.
  • When we send a message to an NSObject, that message looks up in that object’s class object method list.
  • When we send a message to a Class, the message is looked up in the list of methods on the Class’s Meta Class object.

8. Describe the procedure for calling methods in Objective-C

Objective-c is a dynamic language, and each method is dynamically converted to a message at runtime, i.e., objc_msgSend(Receiver, selector). The process is described as follows:

  • When OBJc sends a message to an object, the Runtime library finds the actual class to which the object belongs based on the object’s ISA pointer
  • Then look for methods to run in the list of methods in the class and the list of methods in its parent class
  • If the method is not found in the topmost superclass (typically NSObject), the program will die at runtime and throw an unrecognized selector sent to XXX
  • But before that happens, objc’s runtime will give you three chances to save an application from a crash, and the explanation for those three crashes can be found in the question “When will an Unrecognized Selector exception be reported?”

9. The difference between load and initialize

Both will automatically call the parent class, no super operation is required, and will only be called once (excluding external display calls).

  • The load and initialize methods are both called before instantiating the object, with main being called before and the latter after. These two methods are called automatically and cannot be called manually.
  • The load and initialize methods do not call the parent method as shown, but are automatically called, even if the parent does not have the initialize method. The load method does not call the parent.
  • The load Method is usually used to perform Method Swizzle, and the Initialize Method is usually used to initialize global or static variables.
  • The Load and initialize methods use locks internally, so they are thread-safe. Keep the implementation as simple as possible, avoid blocking threads, and stop using locks.

Objective-c is a dynamic runtime language.

  • The main thing is to defer data type determination from compile time to run time. The problem really involves two concepts, runtime and polymorphism.
  • In simple terms, the runtime mechanism allows us to not determine the class of an object and call the methods specified for that class object until run time.
  • Polymorphism: The ability of different objects to respond to the same message in their own way is called polymorphism.
  • What that means is that suppose the biological class, “life,” has the same method — eat; So humans are living creatures, and pigs are living creatures, and they inherit life, and they implement their own eat, but we just need to call each other’s eat method. That is, different objects respond in their own way to the same message (in response to the selector eat). Therefore, the runtime mechanism is the basis of polymorphism.

More: An iOS Interview Guide