Method Swizzle black Magic, modifying iOS System Class Library methods (reprint)

Source: Internet
Author: User
Tags vars

Generally speaking, the method provided by the system is enough to be developed, but sometimes it is difficult to do it with common methods.

such as: Before all the viewcontroll of the Viewwillappear: The method before a log

You might do this:

1. Build a Uiviewcontroll parent class, override the Viewwillappear method, and call the Super Viewwillappear method before adding a log

2. All new Uiviewcontroller inherit the first step of the generated

It is true that you are doing this function, but you have made so many changes, basically every uiviewcontroller to modify the parent class, this method is too cumbersome

This article provides a simple way to implement

In my understanding, Object-c's class invocation method is defined based on three elements.

1. Method, which represents a method type in a class definition (typedef struct OBJC_METHOD *method)

2. SEL selector (typedef struct OBJC_SELECTOR *sel), a method at run-time name, common to have [self performselector: @selector (somemethod:) withobject : Nil afterdelay:0.5]; @selector (SomeMethod:) as the entrance to the method

3. Implementation of the method entry (typedef ID (*IMP) (ID, SEL, ...) )

These three elements determine which function to call specifically

Look directly at the code

[OBJC]View Plaincopyprint?
  1. #import "Uiviewcontroller+tracking.h"
  2. #import <objc/runtime.h>
  3. @implementation Uiviewcontroller (Tracking)
  4. + (void) load {
  5. nsstring *classname = Nsstringfromclass (self. Class);
  6. NSLog (@ "classname%@", classname);
  7. static dispatch_once_t Oncetoken;
  8. Dispatch_once (&oncetoken, ^{
  9. Class class = [self class];
  10. //When swizzling A-class method, use the following:
  11. //Class class = Object_getclass ((id) self);
  12. SEL originalselector = @selector (viewwillappear:);
  13. SEL swizzledselector = @selector (xxx_viewwillappear:);
  14. Method Originalmethod = Class_getinstancemethod (class, Originalselector);
  15. Method Swizzledmethod = Class_getinstancemethod (class, Swizzledselector);
  16. BOOL Didaddmethod =
  17. Class_addmethod (class,
  18. Originalselector,
  19. Method_getimplementation (Swizzledmethod),
  20. Method_gettypeencoding (Swizzledmethod));
  21. if (didaddmethod) {
  22. Class_replacemethod (class,
  23. Swizzledselector,
  24. Method_getimplementation (Originalmethod),
  25. Method_gettypeencoding (Originalmethod));
  26. } Else {
  27. Method_exchangeimplementations (Originalmethod, Swizzledmethod);
  28. }
  29. });
  30. }

Our category overrides the NSObject Load method OC provides the Objc/runtime.h class let's get these things, and also provides the function of the class method operation

We're thinking of replacing the system directly with a method, and then adding some custom actions to the method

We just wanted to run it once enough, so we used Dispatch_once (&oncetoken, ^{...}
Next, add a new method to the class

Replace the new method with the system method

[OBJC]View Plaincopyprint?
    1. #pragma mark-method swizzling
    2. -(void) Xxx_viewwillappear: (BOOL) animated {
    3. NSLog (@ "viewwillappear:%@", self );
    4. [self xxx_viewwillappear:animated];
    5. }

But when the new method is implemented, the call is [self xxx_viewwillappear:animated]; Maybe you'll wonder

This is because we have used xxx_viewwillappear and viewwillappear in the above exchange. So the actual implementation is the viewwillappear of the system.

This time maybe you have questions again, why the implementation is-(void) Xxx_viewwillappear: (BOOL) animated{} such a

This is because SEL Swizzledselector = @selector (xxx_viewwillappear:); it's our new way of writing.

Can be combined with this blog to see, map is easy to understand

http://blog.csdn.net/yiyaaixuexi/article/details/9374411

And this one is quite clear about SEL.

http://blog.csdn.net/fengsh998/article/details/8612969

Code

Https://github.com/holysin/Method_swizzle

Method Swizzle black Magic, modifying iOS System Class Library methods (reprint)

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.