IOS Runtime principle

Source: Internet
Author: User

For the runtime mechanism, the information found on the Internet is probably how to use these things, and to see the implementation of the Runtime.h header file, of course, this is a good way to learn, but, in fact, we still do not know the runtime is compiled into the C + + language after what to do?
Find a Daniel to the information, immediately to the runtime have a certain understanding!

We casually write a small program, the code is as follows:
The person class header file is as follows,

#import <Foundation/Foundation.h>

@interface Person:nsobject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) int age;

@end

MAIN.M files are as follows

int main (int argc, const char * argv[])

{

Person *p = [[Person alloc] init]; NSString *str = @ "Zhangsan";p. Name = str;//P.name is equivalent to [P SETNAME:STR];p. Age = 20;return 0;

}

Then we open the terminal, locate the CD to the file directory at the command line, and then enter:

CLANG-REWRITE-OBJC main.m

command can compile main.m into C + + code, change to a different file name, will generate different C + + code
This is to generate the main.cpp this C + + file, open the file code
Look at the bottom main function of the main.cpp,
This way we can see how the underlying implementation works!

At this point, we need to know these methods:
Objc_msgsend can send messages to objects
Objc_getclass ("person") can get the object to the specified name
Sel_registername ("Alloc") methods that can be called to an object

By looking at the C + + code, we come to the conclusion that:
Use the Objc_msgsend function to send the method Sel_registername obtained to the object instantiated by the Objc_getclass function
Such a message
Code is for people to see, incidentally let the machine realize the function. In the process of routine development, we should use less runtime,

When will the runtime be used?
Runtime Application Timing:
1> when you need very high performance development, use runtime, note: OC code has not been able to meet performance requirements
2> when we are curious about the implementation of the system, we can use Clang to decompile C + + to see the underlying implementation mechanism!

Finally, I know I wrote this blog may not be very good, or readers think there is something wrong, I hope to point out, we progress together!

The project is to explain the runtime's underlying implementation principle, if you want to know how the runtime is used, you can view the runtime.h header file View!
The following is the runtime mechanism method of some of the use of methods introduced, I hope it is useful for everyone!
Related Technical documents: HTTP://WWW.TUICOOL.COM/ARTICLES/UIMINM
http://blog.csdn.net/lengshengren/article/details/17764135

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.