1, Do we need to release objects associated with the Runtime Associate method when the main object is dealloc?

Not required either under MRC or ARC. Associated objects are released much later in their life cycle than the objects themselves in object_Dispose () method called by nsobject-dealloc.

A:

Call-release: The reference count goes to zero. The object is being destroyed and its life cycle is about to end. No new __weak references, 2, parent class call -dealloc in the most directly inherited parent class call -dealloc if MRC code will manually release the instance variables (iVars) in the inheritance of each layer of the parent class call again Call object_Dispose () method in Objective-C Runtime >4. Call object_dispose() for C++ instance variables (iVars) and call destructors -release for ARC instance variables (iVars) to disengage all use of runtime The Associate method removes all __weak references to the object and calls free().Copy the code

2. The data structure of the instance object?

For details, see the Runtime source file objc-private.h on lines 127-232.

struct objc_object { isa_t isa; / /... }Copy the code

Essentially, the private property of objC_Object has only one ISA pointer. The memory address that points to the class object.

What is Method Swizzling?

Simply put, it’s a method swap

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. By taking advantage of the dynamic characteristics of Objective-C, the corresponding method implementation of selector can be swapped at run time to achieve the purpose of hook method. Each class has a list of methods, storing the Method name and Method implementation mapping, selector is the essence of the Method name, IMP is a bit like a function pointer, pointing to a specific Method implementation, by selector you can find the corresponding IMP. Several ways to change methods

  • Method_exchangeImplementations swaps implementations of two methods
  • Implementation of the class_replaceMethod replacement method
  • Use method_setImplementation to set the IMP of a method directly

4. When is an exception called by an unrecognized selector?

When objC sends a message to an object, the Runtime library will find the class to which the object actually belongs according to the isa pointer of the object, and then run the method in the method list of the class and its parent class. If no corresponding method can be found in the topmost parent class, the runtime library will enter the message forwarding stage. If the three-way message forwarding process is not implemented, the program hangs at run time and throws an exception unrecognized selector sent to XXX.

5, how to giveCategoryAdd attributes? In what form are associated objects stored?

View the knowledge point of the associated object.

Talk more about the association object.

Associated objects are stored in a hash table format in a full

In the singleton of the game.

@interface NSObject (Extension)

@property (nonatomic.copy  ) NSString *name;

@end


@implementation NSObject (Extension)

- (void)setName:(NSString *)name {
    
    objc_setAssociatedObject(self.@selector(name), name, OBJC_ASSOCIATION_COPY_NONATOMIC);
}


- (NSString *)name {
    
    return objc_getAssociatedObject(self.@selector(name));
}

@end
Copy the code

Can you add instance variables to compiled classes? 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 linked list of objc_iVAR_list instance variables and the memory size of instance_size instance variables in the class structure have been determined. At the same time, the Runtime calls class_setVARLayout or class_setWeaklvarLayout to 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.

Class object data structure?

See the Runtime source code for details.

The class object is objc_class.

struct objc_class : objc_object { // Class ISA; Class superclass; // Parent pointer cache_t cache; // formerly cache pointer and vtable methods cache class_data_bits_t bits; // class_rw_t * plus custom RR /alloc flags Used to obtain the address class_rw_t *data() { 
        returnbits.data(); // &fast_data_mask get address value}Copy the code

It has a relatively rich structure. Inherits from the objc_Object structure, so contains isa Pointers

  • isa: points to metaclass
  • superClass: points to the parent class
  • Cache: a cached list of methods
  • data: Data, as the name suggests. It’s an encapsulated oneclass_rw_t

Those iOS development common bottom level questions collection!