One, foreword

This article mainly introduces the underlying principle series – the essence of OC objects, for OC objects most of us understand, but for the underlying and many principles relatively understand very little.

Second, the problem of

1. Struct objc_class structure

2. What’s the difference between ID and instanceType?

Similarities:

  • Instancetype and ID are both universal Pointers to objects.

Difference:

  • Id does not determine the true type of an object at compile time. Instancetype does determine the true type of an object at compile time. Id can be used to define variables, as a return value type, as a parameter type; Instancetype can only be used as a return value type.

3. isKindOfClass | isMemberOfClass

  • IsKindOfClass: Determines whether the object is an instance of the class and its subclasses.
  • IsMemberOfClass: Determines whether the object is an instance of the class.

4. How much memory does an NSObject take up?

Because of addressing capability, 32/8 =4 64/8 =8 CPU bits (32 bits 4 bytes, 64 bits 8 bytes) The system allocates 16 bytes to NSObject objects, which are internal to the system and have objects allocated at least 16 bytes in size. (obtained by the malloc_size function)

5. Where does the isa pointer to the object point to?

The ISA of an instance object points to a class object. The ISA of a class object points to a meta-class object. The ISA of a meta-class object points to a meta-class object of the base class

6. Where is the OC class information stored?

Object methods, properties, member variables, and protocol information are stored in the class object in the class method, and in the meta-class object in the specific value of the member variables in the instance object

7. == and isEqual and isEqualToString

isEqualToString :

  • Compares the contents of two strings.

= = :

  • Compares Pointers to two objects, if strings compare the first address.

isEqual:

  • The default is to compare the memory addresses of two objects. The system’s native classes (NSString, NSArray, etc.) override this method and change the method’s rules.

Compare the contents of two objects, not the memory address.

8. When do I need to override isEqual and hash methods

IsEqual:

  • When you need to determine equality between subclasses, override the isEqual: method

The hash method:

  • If you write subclasses that also need to be added to collection types (NSDictionary, NSSet, etc.), the hash method will also need to be overridden.

Third, the nature of the object

1. The bottom level of Objective-C code

2. Convert Objective-C code to C\C++ code

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main.cpp
Copy the code

NSObject the underlying implementation of the object

Underlying implementation of the Student object

3. Object classification

Instance (instance object)

Class (Class object)

Meta-class (metaclass object)

4. Isa pointer

Isa pointer to find class objects, metaclass objectsThe bit operation finds the concrete class object, the metaclass object

5. Method invocation

6. Isa Superclass Summary

Implementation of Dealloc

The Dealloc implementation mechanism is the focus of the content management part, to understand this point, for a comprehensive understanding of memory management is just very necessary.

1. Dealloc invokes the flow

Call _objc_rootDealloc() first and then call rootDealloc() at which point it will determine whether it can be released based on five main criteria

NONPointer_ISA
weakly_reference
has_assoc
has_cxx_dtor
has_sidetable_rc
Copy the code

4-1. If any of the above five methods exists, the object_Dispose () method is called for further processing. 4-2. If there is no one of the previous five cases, then the free operation can be performed, the C function free().

The execution is complete.

2. Object_dispose () invokes the process.

Call objc_destructInstance() directly. Then call the C function free().

3. Objc_destructInstance () calls the process

If there is C++ related content, call object_cxxDestruct() to destroy the C++ related content. Determine hasAssocitatedObjects and, if so, call object_remove_associations() to destroy the set of operations on the associated object. Then call clearDeallocating(). The execution is complete.

4. ClearDeallocating () calls the process.

Start with sideTable_clearDellocating(). Weak_clear_no_lock is then executed, in which the weak reference pointer to the object is set to nil. Next, table.refcnt.eraser () is executed to erase the reference count for this object from the reference count table. At this point, the Dealloc execution process is complete.