Runtime (i): message mechanism

Source: Internet
Author: User

OC will not be the runtime is not understand OC. Recent projects need to do some hot updates, go with a review.

The first thing to know is the OC message mechanism. Nonsense not much to say, directly on the code and comments.

////main.m//Rumtime////Created by 123 on 16/7/4.//copyright©2016 year yipinbaike. All rights reserved.//#import<Foundation/Foundation.h>#import<objc/message.h>@interfacePerson:nsobject- (void) run;+ (void) run;- (void) EatWith: (nsstring*) food;@end@implementation Person- (void) run{NSLog (@"Object Method Run");}+ (void) run{NSLog (@"class method Run");}- (void) EatWith: (nsstring*) food{NSLog (@"ate:%@", food);}@endvoidText1 ();voidText2 ();intMainintargcConst Char*argv[]) {@autoreleasepool {//Text1 ();Text2 (); }    return 0;}#pragmaMark--Message mechanism/*OC methods and C language functions are not the same, OC at compile time is not known which function is actually called, only the real run time through the function name to find the actual function to invoke the OC language is a message mechanism, for example, in Text1, the sequence of calls from top to bottom three methods. *///1. Test message mechanism (without parameters)voidText1 () {//Instance Object MethodsPerson * person =[[Person alloc]init];    [Person run];    [Person Performselector: @selector (run)];    Objc_msgsend (person, @selector (run)); //Class Object Methods//It is important to note that direct use of objc_msgsend (person, @selector (run)) is not allowed. Because the nature of the invocation of a class method is to convert the class name into a class object. The person is just a class name and cannot call the message function directly objc_msgsend ()Class Personclass = [personclass];    [Personclass Run];    [Personclass performselector: @selector (run)]; Objc_msgsend (Personclass, @selector (Run));}//2. Test message mechanism (with parameters)voidText2 () {//Note that Performselector has a limited number of arguments for this function, which is usually done with a dictionary or an array .Person * person =[[Person alloc]init]; [Person EatWith:@"steamed Bun"]; [Person Performselector: @selector (eatwith:) Withobject:@"steamed Bun"]; Objc_msgsend (person, @selector (eatwith:),@"steamed Bun");}

It is important to note that

1. Run the above code to import #import <objc/message.h>

2. You need to set the property to No (check the message mechanism) in the search msg, Build Setting

Runtime (i): message mechanism

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.