? runtime runtime mechanism

Source: Internet
Author: User

The runtime run-time mechanism fully interpretswhen I first heard of runtime, I was afraid, what a big thing AH!!! Later, began to check some information on the Internet, but only then a few, looked for a long time, but also unintelligible, so more frightened!!!! Later after looking at documents and some foreign Daniel blogs, finally to the runtime has a deeper understanding! So I want to write down these things, hoping to help the readers ...Runtime run-time mechanism detailed interpretation

Catalogue [-]

    • Import
    • Import

We have already talked about a runtime principle, now this article is mainly about what the runtime is and how to use! Hope to help the reader!

First of all, the first question,
1 "Runtime implementation of the mechanism is what, how to use, generally used to do?"
I'm not going to mealy you with this question, just tell everyone,
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, the program runs the process, 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,
Example:
Oc:
[[Mjperson alloc] init]
Runtime:
Objc_msgsend (Objc_msgsend ("Mjperson", "Alloc"), "Init")

A second question
What does runtime use for?? Where do you use them? How to use it?
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 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
      For example: We need to archive the properties of a class when the properties are particularly large, when we will write a lot of corresponding code, but if you use the runtime can be set dynamically!
      For example, the PYPerson.h file looks like this

      Import

@interface Pyperson:nsobject
@property (nonatomic, assign) int age;
@property (nonatomic, assign) int height;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age2;
@property (nonatomic, assign) int height2;
@property (nonatomic, assign) int age3;
@property (nonatomic, assign) int height3;
@property (nonatomic, assign) int age4;
@property (nonatomic, assign) int height4;

@end

and the contents of the PYPERSON.M implementation file are as follows

<!-- lang: cpp -->#import "PYPerson.h"
Import

@implementation Pyperson

  • (void) Encodewithcoder: (Nscoder ) encoder
    {
    unsigned int count = 0;
    Ivar
    ivars = Class_copyivarlist ([Pyperson class], &count);

    for (int i = 0; i<count; i++) {

    // 取出i位置对应的成员变量Ivar ivar = ivars[i];// 查看成员变量const char *name = ivar_getName(ivar);// 归档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 = Span class= "Hljs-number" >0;ivar *ivars = Class_copyivarlist ([Pyperson class], &count); for (int i = 0; i<count; i++) { Span class= "hljs-comment" >//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;
    }

@end

So we can see the case of the archive and the file is actually written by runtime.

Learning, the runtime mechanism must first understand the following questions
1 Related header files and functions
1> header File


    • With the header file, we can view the various methods in the runtime!

2> related 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)
      That's the way we use the runtime mechanism.

3> Correlation function

    • 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_ .....
      This is the function we must know to learn the runtime!

4. Essential Knowledge
1> Ivar: Member variable
2> method: Member Methods
From the example above we see the member variables that we define, and if you are dynamically creating methods, you can use method,

Perhaps, see here, do you have a more in-depth understanding of the runtime? Here, I hope we all communicate with each other! If there is anything wrong, please correct me.

? runtime runtime mechanism

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.