objcsetAssociatedObject
objc_setAssociatedObject is a function in the Objective-C runtime that allows developers to associate arbitrary data with an Objective-C object. This is useful for adding custom properties to existing classes without subclassing or modifying the original class definition. The function is part of the Objective-C runtime library and is declared in the objc/runtime.h header file.
The function signature for objc_setAssociatedObject is as follows:
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);
Here's a breakdown of the parameters:
- object: The Objective-C object to which the data will be associated.
- key: A unique key used to identify the associated data. This is typically a static variable or
- value: The data to be associated with the object. This can be any Objective-C object.
- policy: The association policy, which determines the lifetime of the associated object. The available policies are
To retrieve the associated data, the objc_getAssociatedObject function is used. This function takes the same object
objc_setAssociatedObject is commonly used in categories to add properties to existing classes. For example, a category