IOS development -- simple use of Runtime, associated object, ios -- runtime
1. Method Introduction of the Runtime associated object:
In <objc/runtime. h>, there are three associated methods:
objc_setAssociatedObjectobjc_getAssociatedObjectobjc_removeAssociatedObjects
1.1 set Association
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
The first parameter: id object: indicates the associated source object.
The second parameter: const void * key: obtains the index key of the associated person.
Third parameter: id value: associated
Fourth parameter: objc_AssociationPolicy: association policy. Generally, OBJC_ASSOCIATION_RETAIN_NONATOMIC is used.
Objc_AssociationPolicy is an enumeration type. There are 5 types in total:
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) { OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */ OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object. * The association is not made atomically. */ OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied. * The association is not made atomically. */ OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object. * The association is made atomically. */ OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied. * The association is made atomically. */};
1.2 obtain Association
id objc_getAssociatedObject(id object, const void *key)
Same as above:
The first parameter: id object: indicates the associated source object.
The second parameter: const void * key: obtains the index key of the associated person.
1.3 remove Association
void objc_removeAssociatedObjects(id object)
Only one parameter id object is associated with the source object. The function is to remove all associations of objects.
Ii. Simple Application of the Runtime associated object:
The following uses UIButton as an example to complete a function using an associated object: Add a Category for UIButton, define a method, and use block to implement button click callback.
UIButton + Addition. h
# Import <UIKit/UIKit. h> # import <objc/runtime. h> // import the header file // declare the callback blocktypedef void (^ ButtonClickCallBack) (UIButton * button) for a button click event; @ interface UIButton (Addition) // The added callBack Method for UIButton-(void) handleClickCallBack :( ButtonClickCallBack) callBack; @ end
UIButton + Addition. m
# Import "UIButton + Addition. h "// declare a static index key to obtain the value of the associated object static char * buttonClickKey; @ implementation UIButton (Addition)-(void) handleClickCallBack :( ButtonClickCallBack) callBack {// associate the button instance with the callBack block through the index key: objc_setAssociatedObject (self, & buttonClickKey, callBack, OBJC_ASSOCIATION_RETAIN_NONATOMIC ); // set the button execution method [self addTarget: self action: @ selector (buttonClicked) forControlEvents: UIControlEventTouchUpInside];}-(void) buttonClicked {// use the static index key, get the associated object (here is the callBack block) ButtonClickCallBack callBack = objc_getAssociatedObject (self, & buttonClickKey); if (callBack) {callBack (self) ;}}@ end
In UIViewController, import the UIButton classification header file, define a button object, and call the classification method:
[self.testButton handleClickCallBack:^(UIButton *button) { NSLog(@"click...");
}];
We have completed the simple use of the runtime associated object. For a UIButton click event, we can process the click event in the block callback, instead of calling it again:
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
This improves our development efficiency. We can also do this for UIView and UIAlertView.
Try writing a tool class of your own to develop quickly and efficiently.