Mobile Development 7 min read

Understanding iOS Runtime: Dictionary-to-Object Conversion, Associated Objects, and Method Swizzling

This article explains how iOS developers can use the Objective‑C runtime to automatically convert dictionaries to model objects, add properties to existing classes via associated objects, and implement method swizzling for tasks such as analytics injection, illustrating each technique with code flow and diagrams.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Understanding iOS Runtime: Dictionary-to-Object Conversion, Associated Objects, and Method Swizzling

In everyday iOS development we often need to convert JSON dictionaries to model objects, extend third‑party classes with new properties, or inject custom logic into existing methods without modifying their source code; all of these scenarios can be addressed using the Objective‑C runtime.

Dictionary to Object – By creating an instance of the target class and using class_copyIvarList to retrieve all ivars, developers can iterate over the ivar list, obtain each ivar’s name with ivar_getName , strip leading underscores, and assign values from the dictionary. The article shows the runtime structures ( objc_class , objc_ivar_list , ivar_t ) that make this possible.

The process also handles name mangling (removing the leading underscore) and can be extended to support arrays, nested models, and mismatched server‑client field names, as demonstrated in libraries like YYModel.

Associated Objects – When a category cannot add stored properties, the runtime provides objc_setAssociatedObject and related functions. The article details the three parameters (object, key, value, policy) and shows how the runtime maintains a global AssociationsManager with an AssociationsHashMap to store and retrieve these extra values.

The implementation handles memory management policies (retain, copy) and ensures that existing associations are updated or new ones are created as needed.

Method Swizzling – For injecting behavior such as analytics tracking, developers can swap the implementations of two methods using method_exchangeImplementations . The article walks through a concrete example where a UIViewController category swaps viewDidAppear: with a custom implementation, explaining how the runtime’s method_t structure stores the IMP (function pointer) and how the swap updates the method cache.

These runtime techniques—dictionary‑to‑model conversion, associated objects, and method swizzling—provide powerful, low‑overhead ways to extend iOS apps without altering third‑party code.

Reference Links

Runtime source compilation: https://mp.weixin.qq.com/s/hfwZiKt4D46gg9GOg1ZOsw

YYModel discussion: https://www.jianshu.com/p/d4176577ccd1

Additional blog: https://www.cnblogs.com/DafaRan/p/7878226.html

Mobile developmentiOSruntimeAssociated ObjectsDictionary to ModelMethod Swizzling
JD Retail Technology
Written by

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.

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.