First, the runtime understanding
OC is an object-oriented language This is common sense, in fact, through the runtime mechanism to dynamically create classes and objects, here is simply the use of runtime usage!
Categories (category) Add attribute _ Remember to import header file before use #import <objc/runtime.h>
Dynamic binding properties through these three methods
Objc_exportvoidObjc_setassociatedobject (ID Object,Const void*key,IDvalue, Objc_associationpolicy policy) Objc_available (10.6,3.1,9.0,1.0);//set method
Objc_exportIDObjc_getassociatedobject (ID Object,Const void*key) Objc_available (10.6,3.1,9.0,1.0);//get method
Objc_exportvoidObjc_removeassociatedobjects (ID Object) objc_available (10.6,3.1,9.0,1.0);//Removal
eg
- (void) SetName: (NSString *) name{//objc_association_assign = 0,//Assign//objc_association_retain_nonatomic = 1,//retaion, Nonatomic//objc_association_copy_nonatomic = 3,//Copy, Nonatomic//Objc_association_retain = 01401,//retain//objc_association_copy = 01403//Copy /** Implement the Set method @param object#> self description#> The key value of the Key#> property is uniform on the line description#> @param value The value of the #> property description#> @param policy#> and copy,retain,assign similar to description#>*/Objc_setassociatedobject (self, Namekey, name, objc_association_copy_nonatomic);}//defining the key values of a constant property uniformStatic Char*namekey ="Namekey";-(NSString *) name{/** Implement the Get method @param object#> This is the description#> that fills in the key value of the Key#> property of the @param description#>*/ returnObjc_getassociatedobject (self, namekey);
Three, two methods of execution replacement
SEL originalselector = nsselectorfromstring (@ " Original method name "); = nsselectorfromstring (@ " method name to replace "); class ], originalselector); class ], swizzledselector); method_exchangeimplementations (Originalmethod, Swizzledmethod);
iOS runtime mechanism to add attributes to categories (category), replace the method of the original class execution