Mobile Development 22 min read

Deep Dive into the Objective‑C Runtime: Object Model, Class Structure, Dynamic Loading and Practical Uses

This article provides an in‑depth exploration of the Objective‑C runtime, covering the underlying object model, class and metaclass structures, the isa_t layout, how classes are loaded and initialized, dynamic loading mechanisms, and practical applications such as ORM and automatic NSCoding implementation.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Deep Dive into the Objective‑C Runtime: Object Model, Class Structure, Dynamic Loading and Practical Uses

Objective‑C is a dynamic language whose runtime postpones many decisions to execution time, requiring both a compiler and a runtime environment. The runtime represents classes and objects as C structs, with objc_object containing an isa pointer that links an instance to its class, and objc_class inheriting from objc_object to represent classes themselves.

The isa_t union stores class information in a compact 64‑bit value; its bit‑fields encode flags such as whether the object is a Swift object, reference‑counting details, and other metadata. The class’s bits field points to a class_ro_t (read‑only) structure that holds the class name, ivar layout, method list, protocol list, and property list. At runtime the class_rw_t (read‑write) structure is created from the class_ro_t to allow modifications.

During application launch the dynamic linker dyld loads images and notifies the runtime via _dyld_objc_notify_register . The runtime registers three callbacks: map_images , load_images , and unmap_image . map_images calls map_images_nolock , which ultimately invokes _read_images to register classes, methods, selectors, and protocols. load_images prepares and calls +load methods for each class and category, while initialize is invoked lazily the first time a class’s method is used.

The article also explains how the runtime resolves method calls via objc_msgSend , how super messages are dispatched, and how instance creation allocates memory based on the class’s ivar layout. It describes the process of dynamic loading of new classes or categories at runtime using objc_loadModules and NSBundle .

Beyond theory, the article shows practical uses of the runtime for object‑relational mapping (ORM). It presents a simple implementation that converts a dictionary to a model object by enumerating the class’s properties with class_copyPropertyList and assigning values via KVC. It also demonstrates automatic NSCoding and NSCopying support by iterating over ivars and encoding/decoding each value.

Finally, a small quiz illustrates that adding a class method in a NSObject category works correctly: the method is found in the metaclass hierarchy, and a call to [NSObject foo] succeeds, while invoking the method via performSelector: on an instance also works, confirming the runtime’s method‑lookup rules.

iOSMetaprogrammingruntimedynamic loadingORMObjective-CClass Loading
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.