"Objective-c Runtime Analysis (i)-runtime preliminary"

Source: Internet
Author: User

http://www.tekuba.net/program/335/

Main references in this series:
Objective-c Runtime Reference
Objective-c Runtime Programming Guide
Major documents involved: Objc/message.h,objc/objc-api.h,objc/objc.h,objc/runtime.h
Cool bar [tekuba.net] Use the Creative commons agreement "attribution-non-commercial use-consistent", using the content of this article please follow the agreement
OBJECTIVE-C Runtime is the basic content of objective-c, understanding OBJECTIVE-C Runtime is very useful for mastering the many technical principles of objective-c. Special cool bar specially organized the content of Objective-c runtime, a total of six articles, this article is the first article:
"Objective-c Runtime Analysis (i)-runtime preliminary"
"Objective-c Runtime Analysis (ii)-CLASS,METHOD,SEL,IMP"
"Objective-c Runtime Analysis (iii)-objc_msgsend"
"Objective-c Runtime Analysis (iv)--dynamic Method Resolution"
"Objective-c Runtime Analysis (v)-message Forwarding"
"Objective-c Runtime Analysis (vi)-type encodings & declared Properties"

OneWhat is objective-c Runtime
Simply put, Objective-c is a dynamic language that defers compiling and linking when it's time to run. This means that the Objective-c language requires not only a compilation environment, but also a runtime system (a dynamic language typically has its own runtime system, such as the Java Runtime) to execute compiled code. Objective-c Runtime is like objective-c operating system, OBJECTIVE-C based on Objective-c runtime to work. Therefore, it can be said that the OBJECTIVE-C runtime is the foundation of Objective-c, the soul.

TwoDynamic Language VS static language
Cool Bar in the first contact Objective-c runtime, see the above explanation is still confused, in the end what kind of language is dynamic language? Don't worry, after reading the following example, you should be clear.
Write the following code in the static language C language:

FoldingExpandC + + Codecopy content to clipboard
    1. #include <stdio.h>   
    2. int Main (int argc, const char **argv[])
    3. {
    4. printf ("Hello tekuba!"  );
    5. return  0;
    6. }


Compiling the code will be parsed by the compiler syntax, optimized for translation into machine language, and the system executes your code according to your design logic and top-down.
In objective-c, messages are sent, such as:
[Tekuba Hello];
will be replaced by the compiler Chengcheng:
Objc_msgsend (Tekuba, selector);
If there are parameters, it may be:
Objc_msgsend (Tekuba, selector, arg1, arg2, ...);
Messages are bound to the function implementation only at run time, rather than in the same way as the C language does in a static, compiled logic. That is, the compile phase simply determines that a hello message is sent to the Tekuba object, but it is not sent, and the real send is waiting to run. Therefore, the compile phase is completely unaware of the implementation of the Hello method, even if the method is either implemented or not known. Speaking of which, it is cool to think of the previous contact with Windows programming, there is a very important concept: "Windows message mechanism", the cool bar is the understanding that it is actually a "runtime" mechanism. But because it is not as good as nsobject-c, such as self-examination and other excellent ability, so does not seem particularly prominent.
Other wordsThe so-called "runtime" is actually a management of the operating code environment mechanism, to ensure that the code in the operation of self-examination, the ability to judge。

Threeobjective-c Runtime is open source.
At present, Apple and GNU each maintain an open-source runtime version. Apple's project address is: http://opensource.apple.com/tarballs/objc4/
The Objective-c Runtime is open source and available anytime from http://opensource.apple.com. In fact examining the objective-c are one of the first ways I went through to figure out how it worked, beyond reading APPL ES documentation on the matter. You can download the current version of the runtime (as of this writting) for Mac OS X 10.6.2 here objc4-437.1.tar.gz.
OBJECTIVE-C runtime is implemented by C language. Runtime, as the most central part of Objective-c, is almost entirely implemented by C language. The exception to "almost" here includes methods (such as the Objc_msgsend method) or even a compilation.


Fourinteracting with the OBJECTIVE-C runtime
Reference: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtInteracting.html
OBJECTIVE-C program has three ways to interact with runtime systems [OBJECTIVE-C programs interact with the runtime system at three distinct Levels:through objecti Ve-c source code; Through methods defined in the NSObject class of the Foundation framework; And through direct calls to runtime functions].
1. Objective-c Source Code
In most cases, the runtime system runs automatically in the background, and we only need to write and compile the Objective-c source code.
When compiling objective-c classes and methods, the compiler automatically creates some data structures and functions for implementing the language dynamic characteristics. These data structures contain the information in the class definition and the Protocol class definition,

2, NSObject Methods
Most of the classes in the Cocoa program are subclasses of the NSObject class, so most of them inherit the NSObject class and thus inherit the nsobject behavior (the Nsproxy class is an exception). However, in some cases, the NSObject class simply defines a template that accomplishes something without providing all the required code.
For example, the NSObject class defines the description method, which returns a string representation of the contents of that class. This is mainly used to debug the program: The Print-object method in GDB is to print directly the string returned by the method. The implementation of the method in the NSObject class does not know what is in the subclass, so it simply returns the name of the class and the address of the object. Subclasses of NSObject can re-implement the method to provide more information. For example, the Nsarray class overrides the method to return the contents of each object contained in the Nsarray class.
Some nsobject methods simply get information from the runtime system, allowing the object to perform a certain degree of self-examination. For example, class returns the classes of objects, Iskindofclass: and Ismemberofclass: Checks whether the object is in the specified class inheritance system, Respondstoselector: Checks whether the object responds to the specified message; Conformstoprotocol: Checks whether the object implements the method of the specified protocol class; Methodforselector: Returns the address of the specified method implementation.

3. Runtime Functions
The runtime system is a dynamic library with a public interface, consisting of a collection of data structures and functions, and the Declaration header files for these data structures and functions are in/USR/INCLUDE/OBJC. These functions support the use of pure C functions to implement and objective-c the same functions. There are also functions that form the basis of the NSObject class method. These functions make it possible to access the runtime system interfaces and provide development tools. Although in most cases they are not required in the OBJECTIVE-C program, some functions are sometimes very useful for objecitve-c programs.

Other:
OBJECTIVE-C runtime version and applicable platform, etc.
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ Ocrtversionsplatforms.html
Reprint please indicate from Cool bar, this article address: http://www.tekuba.net/program/335/
Recommended reading:
Instructions for using IOS Nstimer
IOS opens another app in the app
IOS Screen Rotation Control detailed
iOS Internationalization tips

Want to get updates on the cool bar in time? Want to know Ios,android develop the latest technology trends, click or scan the QR code below to download the "read More" App, rich ios,android,web and other fields developers blog with you to subscribe.

Next: objective-c runtime Analysis (ii)-CLASS,METHOD,SEL,IMP

"Objective-c Runtime Analysis (i)-runtime preliminary"

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.