How runtime is used in IOS

Source: Internet
Author: User

What is runtime?

Runtime is a relatively low level of pure C language API, we usually write the OC language code, in the program run process, will be converted to runtime C language code, runtime is OC language behind the workers.

Example:

The code in OC:

[[Zyperson Alloc]init];

In runtime, it turns into this:

Objc_msgsend (Objc_msgsend ("Zyperson", "Alloc"), "Init");

When will the runtime be used?

Runtime application Time:

1. Use runtime when very high performance development is required (the OC code will not be able to meet the requirements at this time)

2. When we are curious about the implementation inside the system, we can use Clang to decompile to C + + to see the underlying implementation principle.

Runtime is the lower level of OC and can perform some very low-level operations (OC is a method that cannot be implemented)

Where runtime is used:

1. Dynamically create a class during the program's run (for example, the underlying implementation of KVO)

2. Dynamically add properties/methods to a class or modify property values/methods while the program is running.

Example: void Swizzle (Class C, sel origsel, sel newsel)
{
Method Origmethod = Class_getinstancemethod (c, Origsel);
Method Newmethod = nil;
if (!origmethod) {
Origmethod = Class_getclassmethod (c, Origsel);
if (!origmethod) {
Return
}
Newmethod = Class_getclassmethod (c, Newsel);
if (!newmethod) {
Return
}
}else{
Newmethod = Class_getinstancemethod (c, Newsel);
if (!newmethod) {
Return
}
}
You already have the added is not successful, direct Exchange can
if (Class_addmethod (c, Origsel, Method_getimplementation (Newmethod), method_gettypeencoding (NewMethod))) {
Class_replacemethod (c, Newsel, Method_getimplementation (Origmethod), method_gettypeencoding (OrigMethod));
}else{
Method_exchangeimplementations (Origmethod, Newmethod);
}
}

When called: Swizzle ([Uiviewcontroller class], @selector (viewdidappear:), @selector (customviewdidappear:)); This method is to use the runtime mechanism to dynamically modify the system's Viewdidappear method.

3. Iterate through all of the member variables (attributes)/methods of a class.

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!

Learn the runtime mechanism first to understand a few questions:

1. Related header files and functions

> View <objc/runtime.h> header file, we can view the various usage methods of runtime.

2. Related Application Scenarios

>nscoding (archive and file, you can use runtime to quickly traverse all the properties of a model object).

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

>KVO (Dynamic generation of a class using runtime)

> for packaging frames (can be completely customized, how to change how to change)

3. Related functions

>objc_msgsend: Sending a message to an object

>class_copymethodlist: Traversing all methods of a class

>class_copyivarlist: Traversing all member variables (attributes) of a class

>class_ ...

4. Essential Knowledge
> Ivar: Member variables
> 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,

How runtime is used in IOS

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.