iOS execution time with method swizzling

Source: Internet
Author: User

C is a static language, and it works by means of function calls, so that at compile time we have determined how the program executes. While Objective-c is a dynamic language, it does not invoke the method of the class to perform the function, but instead sends a message to the object, and the object will find a matching method to execute after receiving the message. This way, the C language works at compile time to be done at the time of execution, to gain additional flexibility.

There is a @selector in Objective-c, which is translated into "selectors" in very many places. In fact, for an instance object of a class, the method of the class is represented by a number, not a long string of strings that we see with this character. This @selector can be used to turn the name of this method into the corresponding number. When a class is determined, the @selector value of each method is actually fixed, and here, you must be able to think of what method swizzling is, yes, suppose we had a method, @selector (a) is a number, After receiving a message, our object finds the appropriate method and executes it-assuming that we replace the number of @selector (B) with the original @selector (a), then the object will execute the B-method even though it receives a message!

In iOS, this is completely achievable, so when do we need to do this? I think there are 2 of times:

1. Crack, needless, this is definitely a weapon to crack, do not explain.

2. During the development and commissioning process, if you are unsure about a method in a library or think you need to expand it, you can write one yourself to replace it. Since Objective-c is a category, it is not necessary to extend the function, but to add it when debugging? Some print statements are very convenient and practical.


For example, NSString inside the lowercasestring method, assuming I am not quite sure what this method is doing, I can write a method to replace it, this method is added to the print statement, so that the log inside a glance.

Need to add first? a nsstring category

@interface NSString (wztest)-(nsstring*) mylowerstring; @end @implementation NSString (wztest)-(nsstring*) mylowerstring{    NSString *lowerstring = [self mylowerstring];    NSLog (@ "%@ =%@", self, lowerstring);    return  lowerstring;} @end
Here's a place to explain that, in the Mylowerstring method, it looks like a recursive call to itself. However, we will use the original lowercasestring method to replace the written mylowerstring method, so here does not call itself, but called the original LowerCaseString method . Please pay attention to this point.     

second, replace the system with the original lowercasestring method, use the method inside the runtime.

    Method Originalmethod = Class_getinstancemethod ([NSString class], @selector (lowercasestring));    Method Swapmethod = Class_getinstancemethod ([NSString class], @selector (mylowerstring));    Method_exchangeimplementations (Originalmethod, swapmethod);    NSString *teststr = @ "This is the Test STRING";    NSLog (@ "lowerstring of teststr=%@", [Teststr lowercasestring]);

let's look at the results of log:

2014-05-29 22:17:55.514 testtableview[1582:a0b] This is the test string = This is the test string

2014-05-29 22:17:55.514 testtableview[1582:a0b] lowerstring of Teststr=this is the test string

We can see that the use of the system is to continue to use the lowercasestring method, but the actual operation is our new method. When you do not need to do this, close method swizzling can be restored.

In our example, we have added a print statement and can actually do a lot of other things as well. This is a very useful method for debugging with a third-party library, which makes it easy to see the contents of variables or do some other work. After debugging, closing method swizzling will work normally.


iOS execution time with method swizzling

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.