What does OC mean when the Dynamic runtime language is? What is dynamic identification, dynamic binding?

Source: Internet
Author: User

Dynamic:

The main is to postpone the determination of the data type by compile time to the runtime.

This problem actually involves two concepts, runtime and polymorphism. Simply put, the run-time mechanism allows us to determine the class of an object until run time, and to invoke the class object to specify the method.

Now, let me think about the dynamic language features of OC. The dynamic characteristics of OC are represented in three aspects: dynamic type, dynamic binding, dynamic loading. It's called dynamic because it has to be done at runtime (run time).

(1) Dynamic type

Dynamic type, that is, the simple point is the ID type. Dynamic types are relative to static types. Explicit basic types like built-in are static types (int, nsstring, and so on). Static types can be identified at compile time. Therefore, if the program has a type that does not correspond, the compiler will issue a warning. The dynamic type is not recognized when compiling the compiler, and it is not recognized until run time, when the program is running, according to the context. So there are two concepts to distinguish: compile-time and run-time.

ID obj = someinstance;

if ([obj Iskindofclass:someclass]) {

SomeClass *classspecifiedinstance = (SomeClass *) obj;

}

(2) Dynamic binding

Dynamic binding is seemingly more difficult to remember, but in fact it is very simple, just remember the keyword @selector/sel. First look at "functions", for some other static language, such as C + +, generally at the time of compiling the function that will be called to the function of the signature of the compiler. Static, cannot be changed. In OC, there is no concept of function, we call "message mechanism", so-called function call is to send a message to the object. At this point, the dynamic binding feature comes. OC can skip compiling, add function calls dynamically when running, decide what method to invoke at runtime, and what parameters need to be passed in. This is dynamic binding, and to implement it, you must bind a method with the SEL variable. The resulting SEL variable represents a reference to a method. One thing to note here is that the SEL is not a function pointer in C, though much like it, but it's really not a function pointer. The SEL variable is just an integer, which is the ID of the method, @selector () is the number of the class method. The previous function call is based on the function name, which is the string to find the function body. But now, we are looking for a method based on an ID integer, and the search word for integers is naturally much faster than a string lookup! Therefore, the specific dynamic binding is not only convenient, but also more efficient.

Due to the dynamic characteristics of OC, the concept of "function" is rarely mentioned in OC, the traditional function is usually compiled with the parameter information and function implementation into the compiled source code, and the most commonly used in OC is the message mechanism. The method of invoking an instance is to send a message to the instance's pointer, and the instance, after receiving the message, looks for a way to respond to the message from its own implementation

(3) Dynamic loading

It is easy to understand that the required resources are loaded on demand, which is basically adapted to different models for iOS development. The most classic example is the loading of @2x images on the retina device, and the loading of the original image on an older normal screen device.

What does OC mean when the Dynamic runtime language is? What is dynamic identification, dynamic binding?

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.