Runtime operating mechanism

Source: Internet
Author: User

What is the implementation mechanism of runtime? What do you usually use? The following author share to everyone, there are any shortcomings, I hope you point to suggestions.


Runtime is a relatively low level of pure C language API, belongs to a C language library, contains a lot of the underlying C language API.

In the OC code that we normally write, the process of running the program eventually turns into the C language code of runtime, the runtime is a behind-the-scenes worker of OC,


For example, in the following method of creating an object

Oc:

person = [Person alloc] init];


Runtime

Objc_msgsend (Objc_msgsend ("person", "alloc"), "Init")


Is it used to dry up?

1. Dynamically create a class in the process of running the program (for example, the KVO implementation)

2. In the process of running the program, dynamically add a property/method to a class, modify the property value/method

3. Iterate through all the member variables (attributes)/methods of a class: columns such as: We need to archive the properties of a class, the properties are particularly large, when we write a lot of corresponding code, but if we use the

Runtime can be set up dynamically,

For example: Person.h file is shown below

@property (nonatomic,assign) int age;

@property (nonatomic,assign) int height;

@property (nonatomic,copy) NSString *name;

@property (nonatomic,assign) int age2; @property (nonatomic,assign) int heigh2t;

@property (nonatomic,assign) int age3;

@property (nonatomic,assign) int heigh3t;

@property (nonatomic,assign) int age4;

@property (nonatomic,assign) int heigh4;

The contents of the PERSON.M implementation file are as follows

  • (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;
    }




Learning, the runtime mechanism must first understand the following questions


1. header files, using header files We can view the various methods in the runtime!


2. Related applications

(1) nscoding (archive and file, use Runtime to traverse all properties of the model object)

(2) Dictionary---model (use runtime to traverse all properties of the model object, remove the corresponding value from the dictionary according to the property name, set to the properties of the model)

(3) KVO (using runtime to generate a class dynamically)

(4) for packaging frame (how to change how to change it)

That's the main direction of the runtime mechanism.

3. Related functions

(1) Objc_msgsend: Send Message to Object

(2) Class_copymethodlist: Traverse all methods of a class

(3) Class_copyivarlist: Traverse all member variables of a 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, if you are dynamically creating a method, you can use method, and perhaps, see here, do you have a more in-depth understanding of runtime?





Runtime operating 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.