Runtime run-time mechanism fully read

Source: Internet
Author: User

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++) {

    //Remove member variables corresponding to position IIvar Ivar = ivars[i];//View member variablesConst Char*name = Ivar_getname (Ivar);//ArchiveNSString*key = [NSStringStringwithutf8string:name];IDValue = [ SelfValueforkey:key]; [Encoder Encodeobject:value Forkey:key];

    }

    Free (ivars);
    }

  • (ID) Initwithcoder: (Nscoder *) decoder
    {
    if (self = [super init]) {

    unsigned intCount =0; Ivar *ivars = Class_copyivarlist ([Pyperson class], &count); for(inti =0; i<count; i++) {//Remove member variables corresponding to position IIvar Ivar = ivars[i];//View member variables    Const Char*name = Ivar_getname (Ivar);//Archive    NSString*key = [NSStringStringwithutf8string:name];IDValue = [Decoder decodeobjectforkey:key];//Set to member variable body[ SelfSetvalue: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)
    • used to encapsulate the frame (how to change it)  
      This is our runtime mechanism, just use the direction

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,

Runtime run-time mechanism fully read

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.