Message Mechanism and Runtime mechanism of runtime

Source: Internet
Author: User

Message Mechanism and Runtime mechanism of runtime
What is Runtime?

Runtime is short for Runtime.
Objective-C is a runtime language. The so-called runtime determines the object type only when the program is running and calls the method corresponding to the class and object.

Message Mechanism

In Objective-C, function calling is called message sending, that is, objc_msgSend, which is a dynamic call process.
The real implementation of message sending and method is bound at the program running stage, rather than the compilation stage.
The compiler converts the message to the call of the objc_msgSend method.

Message function objc_msgSend

In Objective-C, we call a function as follows:

[someObject messageName:parameter]; 


When the compiler sees the code above, it will convert it into a standard C language function call. The called function is the core function in the message mechanism, that is, objc_msgSend. As follows:
objc_msgSend(someObject,@selector(messageName:),parameter); 


The objc_msgSend function contains two necessary parameters: the aggreger and the method name (selector), namely, objc_msgSend (selector );
Of course, you can also accept any number of parameters in the message, that is, objc_msgSend (receiver er, selector, arg1, arg2 ,...);

The message function objc_msgSend performs all required for dynamic binding:
1. It first finds the method implementation corresponding to selector. Because different classes may have different implementations for the same method, the method implementation depends on the type of the message receiver.
2. Then, the Message Receiver object (pointer to the Message Receiver object) and the parameters specified in the method are passed to the find Method for implementation.
3. Finally, return the return value of the method implementation as the return value of the function.

Objc_msgSend manual call

Create a Person class as follows:
Person. h

#import <Foundation/Foundation.h>@interface Person : NSObject  @property (nonatomic, assign) int age;@end

Manually call the objc_msgSend method in the viewDidLoad method of the controller. Pay special attention to importing the objc/runtime. h header file, as shown below:
# Import "ViewController. h "# import" Person. h "# import <objc/runtime. h> // import the runtime header file @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; Person * p = [[Person alloc] init]; objc_msgSend (p, @ selector (setAge :), 18); // corresponding to p. age = 18; NSLog (@ "p Age: % d", p. age) ;}@ end

Run and print the result as follows:
16:12:01. 105 The Age Of objc_msgSendTest [4461: 70b] p is: 18


Personal Original, welcome to reprint, reprint please indicate the source: http://blog.csdn.net/iosHot

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.