Swizzle Interchange Method Name

Source: Internet
Author: User

In iOS runtime there is a function for exchanging method names, called Swizzle, and the following example swaps Imagewithname: With Imagenamed: Two methods to invoke the System method imagenamed: The actual call is Imagewithname:, all the picture names are stitched _os7, when the old project needs to change a set of pictures can be avoided one by one.

@implementation UIImage (Extension)/** *  as long as the classification is loaded into memory, it is called 1 times */+ (void) load{    Method othermehtod = Class_ Getclassmethod (Self, @selector (imagewithname:));    Method Originmehtod = Class_getclassmethod (self, @selector (imagenamed:));    Exchange 2 methods of implementation    Method_exchangeimplementations (Othermehtod, originmehtod);} + (UIImage *) Imagewithname: (NSString *) name{    BOOL iOS7 = [[Uidevice currentdevice].systemversion Floatvalue] >= 7.0;    UIImage *image = nil;    if (iOS7) {        nsstring *newname = [name stringbyappendingstring:@ "_os7"];        Image = [UIImage imagewithname:newname];//here is actually called when the system method imagenamed:    }        if (image = = nil) {        image = [UIImage Imagewithname:name];    }    return image; @end


Swizzle Interchange Method Name

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.