Understanding Objective‑C Runtime: objc_object, objc_class, and Core Data Structures
This article explains the internal data structures of the Objective‑C runtime, covering objc_object, objc_class, class_data_bits_t, class_rw_t, and class_ro_t, and illustrates how these structures define objects, classes, method caches, and associated metadata in iOS development.
We first look at the overall Runtime data structure, as shown in the diagram.
Initial view of objc_object
All objects we use are of type id, which in the runtime is defined as the objc_object struct.
The objc_object struct contains five parts:
1. isa_t type
isa2. isa operation related
3. Weak‑reference related methods – indicate whether the object contains weak references
4. Associated‑object related methods – indicate whether the object has associated objects
5. Memory‑management related methods – the methods we commonly use for memory handling
What is objc_class?
In Objective‑C, a Class corresponds to objc_class in the runtime. The diagram shows that objc_class inherits from objc_object, thus containing an isa pointer and all methods of objc_object. Therefore a class can also be regarded as an object.
The article focuses on the three most important data members of objc_class:
1. Class superclass – the parent class.
2. cache_t cache – a method cache used during message dispatch to improve performance; it is an expandable hash table storing bucket_t entries (key = selector, IMP = function pointer).
3. class_data_bits_t bits – a core part of the class structure, essentially an optimized isa pointer.
class_data_bits_t bits
The struct contains a single member uintptr_t bits (an unsigned long, 8 bytes on arm64). This 64‑bit field stores both a pointer and class‑related flags; for example, the class_rw_t* pointer is obtained via (bits & FAST_DATA_MASK).
class_rw_t definition
The most important members of class_rw_t are: class_ro_t *ro – read‑only class information. method_array_t method – method list. property_array_t properties – property list. protocol_array_t protocols – protocol list.
Pointers related to inheritance: firstSubclass, nextSiblingClass. demangledName – the symbol name of the class.
class_ro_t *ro constant pointer
This pointer refers to read‑only data. Its definition includes fields such as: name – class name. baseMethodList – method list. baseProtocols – protocols. ivars – instance variables. baseProperties – properties.
method_list_t and method_t method_list_t is an array of method_t, which represents a typical function with fields for name, return type, implementation pointer, and parameters.
In summary, this article introduces the definitions and data structures of objects, classes, and methods in the Objective‑C runtime, and provides links to the Runtime source code for further exploration.
Reference links:
https://mp.weixin.qq.com/s/hfwZiKt4D46gg9GOg1ZOsw
https://blog.csdn.net/u013378438/article/details/80493158
https://www.jianshu.com/p/75c0d214dc8b
https://www.jianshu.com/p/13457a27624c
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
