IOS Runtime-oc Black Magic

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.