Use method swizzling to extract duplicate operations

Source: Internet
Author: User

Use method swizzling to extract duplicate operations

by Wusheying

Repeat operations such as data escalation, public features, and so on.

1. Establishing the category of Uiviewcontroller

2. Code:

#import "Uiviewcontroller+analytics.h" #import <objc/runtime.h>  @implementation uiviewcontroller (Analytics)+ (void) Load {Static dispatch_once_tOncetoken;dispatch_once(&oncetoken, ^{class class = [ SelfClass]; SEL Originalselector =@selector(viewwillappear:); SEL Swizzledselector =@selector(ana_viewwillappear:);        Method Originalmethod = Class_getinstancemethod (class, Originalselector); Method Swizzledmethod = Class_getinstancemethod (class, Swizzledselector);BOOLSuccess = Class_addmethod (class, Originalselector, Method_getimplementation (Swizzledmethod), method_gettypeencoding (Swizzledmethod));if(success) {Class_replacemethod (class, Swizzledselector, Method_getimplementation (Originalmethod), method_gettypeencoding        (Originalmethod)); }Else{method_exchangeimplementations (Originalmethod, Swizzledmethod); }    });} - (void) Ana_viewwillappear: (BOOL) Animated {[ SelfAna_viewwillappear:animated];NSLog(@"Ana_viewwillappear");NSLog(@"%@", Self);    Mta_report (); [ SelfHidenavigationshadowline];}@end

3. Output
2015-06-16 09:17:59.838 test[37292:2748514] Ana_viewwillappear
2015-06-16 09:17:59.838 test[37292:2748514] viewcontroller:0x7fb7515291c0>

Use method swizzling to extract duplicate operations

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.