Ios:runtime Run time

Source: Internet
Author: User

1. What is a runtime runtime?

Runtime is a relatively low level of pure C language API, belongs to 1 C language Library, contains a lot of the underlying C language API.
In the OC code that we usually write, in the process of running the program, in fact, it turns into the runtime's C language code, runtime is OC's behind-the-scenes workers


For example, in the following method of creating an object, the Alloc method and the Init method are ultimately implemented by sending a message at runtime (message mechanism)

1 // OC: 2 [[Ljperson alloc] init];   3//Runtime: Import <objc/runtime.h> 4 objc_msgsend with runtime (objc_ Msgsend ("Ljperson", "Alloc"), "Init");

What's the use of 2.runtime?
Runtime is the bottom of the OC and can perform some very low-level operations (with OC is not realistic, bad implementation)

    • Dynamically create a class during the program's run (for example, the KVO implementation)

    • Dynamically adding properties \ Methods to a class, modifying property values \ Method \ method implementation during program run

    • Traverse all member variables (properties) of a class \ All methods
      * Code can refer to Mjextention framework

    • Exchange two implementations of the methods (mainly used to make some modifications to the system's own methods)

Functions commonly used in 3.runtime:

    • Objc_msgsend: Sending a message to an object
    • Class_copymethodlist: Traversing all methods of a class
    • Class_copyivarlist: Traversing all member variables of a class
    • Class_getclassmethod: Obtaining class methods for a class
    • Class_getinstancemethod: Obtaining an object method for a class
    • Class_getmethodimplementation: Obtaining the implementation of a method of a class ;
    • Method_setimplementation: Setting the implementation of a method
    • Method_exchangeimplementations: Implementation of two methods of exchange

4. Application Examples:

Waiting to be written ...

Ios:runtime Run time

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.