OBJECTIVE-C Runtime

Source: Internet
Author: User

Runtime: Run-time mechanism
The first thing to understand:
1. What is
1> 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
2> usually write the OC code, in the process of running the program, in fact, eventually turned into the runtime's C language code, runtime is OC's behind-the-scenes workers
Here is an example, as mentioned in the previous article! We can see the underlying file by compiling it into C language.
Oc:
[[Person alloc] init]
When the person object above is created,
Runtime:
Objc_msgsend (Objc_msgsend ("person", "alloc"), "Init")

And the above part just understand the most basic principle, then runtime and what is the deeper use of it?
2.runtime used? What's the use? What can I do for you?
What we need to understand is:
1> runtime is the lower level of OC, can perform some very low-level operations (with OC is not realistic, bad implementation can be achieved through runtime)

    • Dynamically create a class during the program's run (for example, the KVO implementation)
    • Dynamically add properties \ Methods to a class while the program is running, modify property values \ Methods
    • Traverse all member variables (properties) of a class \ All methods

3. Related header files and functions
1> header File


    • Opening the header file, we found a lot of methods, but the most we used is the following function,
      Related functions
    • 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_ .....

Of course, when it comes to using these things, we first have to understand something,
Essential knowledge
1> Ivar: Member variable
2> method: Member Methods

2> Runtime related practical applications

      • Nscoding (archive and file, use Runtime to traverse all properties of Model objects)
      • Dictionary –> model (use Runtime to traverse all properties of the model object, remove the corresponding value from the dictionary based on the property name, set to the properties of the model)
      • KVO (using runtime to generate a class dynamically)
      • For packaging frames (how to change them)

The code below is a piece of code that uses the runtime mechanism when archiving a file, so you don't have to assign a value to each property,

<!--lang:cpp-->-(void) Encodewithcoder: (Nscoder *) encoder{unsigned int count = 0;ivar *ivars = class_copyivarlist ([Pyperson class], &count); for (int i = 0; i<count; i++) {    //Remove the member variable corresponding to the I position    Ivar Ivar = ivars[i];    View member variables    const char *name = Ivar_getname (Ivar);    Archive    NSString *key = [NSString stringwithutf8string:name];    ID value = [self valueforkey:key];    [Encoder encodeobject:value Forkey:key];} Free (ivars);}

(ID) Initwithcoder: (Nscoder *) decoder{if (self = [super init]) {unsigned int count = 0;ivar *ivars = class_copyivarlist ([PY Person class], &count), for (int i = 0; i<count; i++) {    //Remove the member variable corresponding to the I position    Ivar Ivar = ivars[i];    View member variables    const char *name = Ivar_getname (Ivar);    Archive    NSString *key = [NSString stringwithutf8string:name];    ID value = [decoder decodeobjectforkey:key];    Set to member variable body    [self setvalue:value forkey:key];                            }   Free (ivars);               } return self; }

OBJECTIVE-C Runtime

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.