Objective-C Programming Summary (article 6) runtime operations-method exchange

Source: Internet
Author: User

The following describes the runtime behavior of the OC class. This includes method replacement during running, message forwarding, and dynamic attributes. These are still very useful for the love of Aspect-Oriented Programming AOP. Of course, there are many other functions, such as configurable programming. However, according to my previous programming experience in Java and DOTNET, these are all cost-effective. So tryProgramStarts partial completion operations, not for program behaviorCode.

The first piece of code is method exchange. The following example replaces the [nsstring stringbyappendingpathcomponent] method with your own code.

Here is the replacement code:

 
Nsstring * nsstringstringbyappendingpathcomponent (ID self, Sel _ cmd, nsstring * path) {// This definition form is recommended in the development documentation. In fact, this is not necessary in the case of method exchange, you can even give (). however, if the method you want to replace does not actually exist, this definition form is required. Nslog (@ "this is a fake imp for method % @", nsstringfromselctor (_ cmd); nslog (@ "I won't do anything! But I will return a virus !"); // Logic return [nsstring stringwithcstring: "virus !!!" Encoding: nsutf8stringencoding];}

The code for method exchange is as follows:

Class strcls = [nsstring class];

Sel oristringbyappendingpathcomponent = @ selector (stringbyappendingpathcomponent :);

Class_replacemethod (strcls,

Oristringbyappendingpathcomponent,

(IMP) nsstringstringbyappendingpathcomponent,

Null );

// The following type parameter can be null. If the method to be replaced does not exist, the class_addmethod is called and the type parameter is used to set the method to be added.

/*

the description of Apple development reference is as follows:

type parameter: an array of characters that describe the types of the arguments to the method. for possible values, see Objective-C runtime programming guide type encodings. since the function must take at least two arguments- Self and _ CMD , the second and third characters must be "@:" (the first character is the return type ).

  • If the method identified by name does not yet exist, it is added as if class_addmethod were called. the Type encoding specified by types is used as given.

  • If the method identified by name does exist, its imp is replaced as if method_setimplementation were called. the Type encoding specified by types is ignored.

*/

Related Article

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.