Adhere to the growth of a daily preface
One of the features that we often overlook in runtime is the association object feature.
An association object is similar to a member variable, but the associated object is added after the class creates an instance. Therefore, it is not possible to obtain the relevant information through runtime's method of acquiring member variables.
Note: The operation of the associated function can only be performed by the following C function.
Set the associated object
void Objc_setassociatedobject (ID object, const void *key, ID value, Objc_associationpolicy policy);
Gets the associated object
ID objc_getassociatedobject (ID object, const void *key);
Remove an associated object
In the case of a system class if we want to add a property to a class, since we cannot add member variables to the category, we can only do so by creating subclasses of the class.
Objective-c for this issue, a solution is provided: the association objects (associated object).
We can think of an associative object as a Objective-c object (such as a dictionary) that is connected to an instance of a class by a given key. However, because the C interface is used, key is a void pointer (const void *). We also need to specify a memory management policy to tell the runtime how to manage the memory of this object. This memory-managed policy can be specified by the following values:
enum {
objc_association_assign = 0,//weak reference, object destroy does not cause reference count change of associated object
objc_association_retain_nonatomic = 1,//strong reference, Multithreaded security not supported
objc_association_copy_nonatomic = 3,//deep copy, multithreaded security not supported
Objc_association_retain = 01401,//strong reference support for thread safety
objc_association_copy = 01403 //deep copy, support thread safety
};
Example
To create a UIView classification, we want to add a click Gesture callback code block to the category.
Uiview+tap.h
#import <UIKit/UIKit.h>
@interface UIView (TAP)
-(void) Settapactionwithblock: (void (^) (void)) block;
@end
uiview+tap.m
#import "Uiview+tap.h" #import <objc/runtime.h> #define Keyforaction "action" #define Keyforrecognizer " Recognizer "@implementation UIView (TAP)-(void) Settapactionwithblock: (void (^) (void)) block {//If the associated object is not used here, you may need to keep removing
Gesture, and then create the gesture UITapGestureRecognizer *gesture = Objc_getassociatedobject (Self,keyforrecognizer); if (!gesture) {gesture = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Actionfort
Apgesture:)];
[Self addgesturerecognizer:gesture];
Objc_setassociatedobject (self, keyforrecognizer, gesture, objc_association_retain);
}//Here you can successfully add an associated object to the back of the gesture callback function, without having to pass global variables or member variables.
Objc_setassociatedobject (self, keyforaction, block, objc_association_copy); }//Implement callback function-(void) Actionfortapgesture: (UITapGestureRecognizer *) gesture {if (gesture.state = = Uigesturerecognizersta terecognized) {//We can only remove the associated object Void (^action) (void) by Objc_getassociatedobject = Objc_getassociatedobject (Self, KEYFOraction);
if (action) {action (); }}} @end
Test inside the Controller
-(void) viewdidload {
[super viewdidload];
This realizes that does not need to create the classification, very good expands the existing UIView class.
[Self.view settapactionwithblock:^{
NSLog (@ "is tap");}
];
}
Run the program and we'll see the output when we click on Control view
2015-09-15 09:59:21.869 test[5410:381831] is tap
2015-09-15 09:59:23.005 test[5410:381831] is tap
2015-09-15 09:59:23.545 test[5410:381831] is tap
Summary: Association objects are a very useful runtime feature. Can be very good for us to solve the development encountered many such as value, extension objects and other issues.