Deep Dive into iOS Object Model: Instance, Class, and Meta-Class Analysis
This article explores the underlying principles of Objective-C objects, analyzing instance objects, class objects, and meta-class objects through memory allocation, data structures, and pointer relationships.
This article provides an in-depth analysis of Objective-C's object model, focusing on three main object types: instance objects, class objects, and meta-class objects. The author begins by examining how instance objects are allocated in memory, explaining that while the operating system allocates 16 bytes for NSObject objects, only 8 bytes are actually used in 64-bit environments.
The article then explores how Objective-C objects are compiled into C++ structures, revealing that each instance object contains an isa pointer and potentially other member variables. The author explains the difference between class_getInstanceSize and malloc_size, demonstrating that the former returns the size of instance variables while the latter returns the actual memory allocated by the operating system.
Moving to object classification, the article discusses how Objective-C objects are categorized into three types and explains their relationships. The author provides detailed analysis of class objects and meta-class objects, showing that class objects store instance methods, properties, and protocols, while meta-class objects store class methods.
The article includes practical demonstrations using Xcode's LLDB debugger to verify pointer relationships. It explains the isa pointer mechanism, showing how instance objects' isa pointers point to class objects, class objects' isa pointers point to meta-class objects, and meta-class objects' isa pointers point to the base meta-class (NSObject's meta-class).
Superclass relationships are also explored, demonstrating that subclass class objects point to parent class objects, and parent class objects point to the base class (NSObject). The article concludes with a comprehensive summary of the object model, including memory allocation rules, pointer relationships, and the overall structure of Objective-C's runtime system.
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.
