Runtime-ios black Magic, still very fun, message mechanism, method replacement simple record a little, continuous update ....
1. Method substitution
In the class load method, replace the system method
+ (void) load{Method Oldcolormethod= Class_getinstancemethod ([selfclass], @selector (setbackgroundcolor:)); Method Newcolormethod= Class_getinstancemethod ([selfclass], @selector (xg_setbackgroundcolor:)); ///Exchange Method///calling the system's SetBackgroundColor method executes the Xg_setbackgroundcolor methodmethod_exchangeimplementations (Oldcolormethod, Newcolormethod);}- (void) Xg_setbackgroundcolor: (Uicolor *) color{///do not call the system's SetBackgroundColor method in this method to prevent circular referencing[self xg_setbackgroundcolor:color];}
2. Dynamically generated properties
-(void) Setnamexg: (NSString *) namexg{ "namexg", Namexg, objc_ association_retain_nonatomic);} -(NSString *) namexg{ return"namexg");}
3. The realization of the dictionary turn model
#import "Nsobject+xgdictionary.h"#import<objc/runtime.h>@implementationNSObject (xgdictionary)+ (Instancetype) xg_modelkeyvalues: (Nsdictionary *) dicdata{IDModel =[[Self alloc]init]; unsignedintCount =0; ///returns all properties and variables for a classIvar *ivarsa = Class_copyivarlist (self, &count); for(inti =0; I < count; i + +) {Ivar IV=Ivarsa[i]; NSString*ivarname =[NSString Stringwithutf8string:ivar_getname (iv)]; Ivarname= [Ivarname Substringfromindex:1]; IDValue =Dicdata[ivarname]; [Model Setvalue:value Forkeypath:ivarname]; } returnmodel;}@end
The things that ios-runtime ... Edit in ....