My runtime understanding and usage

Source: Internet
Author: User

1. What is a run-time mechanism

1) runtime mechanism is a low-level pure C language API document, belonging to the C language library, contains many of the underlying C language API

2) object in OC, method at run time to know what type it is, this type is dynamically generated like KVO's bottom implementation is the use of runtime.

3) The code that we normally encode will eventually be translated into runtime's C language, and runtime is a behind-the-scenes worker.

As in the official API documentation:

[[Person alloc] init] will eventually be converted to obje_msgsend ("person", "alloc"), "Init");

2. How do I use the runtime?

1) Like some of the things that belong to OC, you can create a class dynamically when the layer is running, that is, a derived class of KVO.

2) dynamically add attribute methods to a class, modify property values and methods like archive in a program run over layer: Dictionary to model that's it.

Archive: Access to data and read data, i.e. archive and file

Cases:

Need to import <objc/runtime.h>

-(void) Encodewithcoder: (Nscoder *) encoder

{

unsigned int count = 0;

Ivar *ivars = Class_copyivarlist ([Ljperson class], &count);

Traverse

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

{

Remove the member variable corresponding to the file

Ivar Ivar = ivars[i];

View member variables

const char name = Ivar_getname (Ivar);

Archive

NSString *key = [NSString stringwithutf8string:name];

ID value = [self valueforkey:key];

}

Release

[Free Ivars];

}

Solution file

-(ID) Initwithcoder: (Nscoder *) decoder

{

if (self = [super init])

{

unsigned int count = 0;

IVar *ivars = Class_copyivarlist ([Ljperson class], &count);

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

{

Remove the member variable corresponding to the file

Ivar Ivar = ivars[i];

View member variables

const char *name = Ivar_getname (Ivar);

NSString *key = [NSString stringwithutf8string:name];

ID value = [self valueforkey:key];

[Self setvalue:value forkey:key];

}

return Free[ivars];

}

return self;

}

3) production see hidden member variables and member methods

Code implementation: #import <objc/runtime.h>

+ (void) initalize

{

unsigned int count = 0;

Copy a list of member variables

Ivar *ivars = Class_copyivarlist ([Uitextfield class], count);

Traverse

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

{

Remove member Variable

Ivar Ivar = * (Ivars *i);

Print

NSLog (@ "%s", Ivar_getname (Ivar));

}

}

About Objc_msgsend: Sending messages to objects

Massage send what happens if the corresponding object is not found

(1) If the instance method is dropped with an example object, it will be manipulated to the object pointed to by the ISA pointer of the instance object.

(2) If a class method is used, it will be found in the object pointed to by the ISA pointer of the class object.

if (1) does not find the method that will manipulate the object Liebi if the corresponding object is not found, it will be found in the cache list in the object.

The corresponding z-class kernel method has not been found to perform (1) (2) steps in the parent class

And so on, if it's not found, it's blocked.

If it is not found in the object method, it will be found in the pointer that the parent class points to.

My runtime understanding and usage

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.