Some strange features are often found in the project. Not very able to find the source of the code, but the implementation of the function, it is possible that this is useful to run the thing.
Runtime is run. It's fun to use. More understanding also helps to understand OC.
Here I write a demo https://github.com/WangFeiF/WFRuntime about runtime.
In this blog is just a summary of the demo. The demo can be downloaded in detail. View source code.
viewcontroller.m//runtime////Created by Wang Fei on 16/5/8.//copyright©2016 year Com.wangfei. All rights reserved.//#import "ViewController.h" #import <objc/runtime.h> #import "WFPerson.h" #import "uiimage+ Swizzling.h "@interface Viewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview *imageorigin; @property (Weak, nonatomic) Iboutlet Uiimageview *imageswizzling; @end @implementation viewcontroller-(void) viewdidload {[Super ViewDid Load]; [Self sendMessage]; [Self methodswizzling];} -(void) SendMessage {#pragma message mechanism Class Classperson = Objc_getclass ("Wfperson"); Create an instance object with the same class//if there is an error here, please change Build Setting, Enable Strict Checking of Objc_msgsend Calls to NO wfperson *person = Objc_msgsend (Classperson, @selector (alloc)); person = objc_msgsend (person, @selector (init));//[Person eat]; Objc_msgsend (person, @selector (runwithcount:), 10); Objc_msgsend (person, @selector (eat)); #pragma dynamic Association method/** * Person object has no SayHello method This time with Wfperson internal Hello instead of SayHello */objc_msgsend (person, @selector (SayHello)); #pragma message forwarding/** * person The method issued to Stoke, which was manipulated by Black magic, was transferred to touch/objc_msgsend (person, @selector (Stoke));} #pragma method Exchange detailed operation see uiimage+swizzling classification-(void) methodswizzling {UIImage *image = [UIImage imagenamed:@ "hitlive"]; _imageorigin.image = image; NSLog (@ "--------This is the dividing line---------"); UIImage *imge = [UIImage tuc_imagenamedmethod:@ "hitlive"]; _imageswizzling.image = Imge;} @end
and the dynamic association properties in runtime to view blogs: http://blog.csdn.net/qq_16844001/article/details/51122074
IOS Runtime-oc Black Magic