About the runtime of iOS

Source: Internet
Author: User

Runtime is a very interesting thing, if you learn iOS development very often will use or be asked to runtime. So what is runtime and how to get to know it.

Runtime: Chinese name runtime, the system in the compilation of some of the types left, the operation in the run time to analyze the dynamic, processing, which also shows that object-c is a dynamic language. (Swift differs from that, although Swift can be called OC's runtime, but Swift itself is a static language.) But by being able to interact with OC to become a static language with dynamic characteristics, this is gossip, no nonsense.

To understand runtime, know what runtime is. I think the key is to look at the objc/objc.h,objc/runtime.h file.

The first thing to know is that Object-c is written in c,c++.

A. Objc.h:

1, the Objc.h file defines an ID is a objc_object struct pointer type, the structure of another ISA such member, ISA is a class type. and class is Objc_class * type

In other words, an object in OC is actually a member of the objc_class*, and the member of this objc_class* type explains what the object is, including what type of object it is, and what method it has. Method parameter composition, what protocol, category, etc.

typedef struct objc_class *class;

struct objc_object {

Class Isa objc_isa_availability;

};

2. Then we'll look at the class type, class, which is the alias of objc_class*. The specific meaning is not that it is to use objc_class the corresponding pointer type of the structure.

3. Then sel, similar to class This is also the alias of another struct pointer, the corresponding structure is called Objc_selector.

Where objc_object can see the specific structure in runtime. Objc_selector I did not find where, who saw a reminder oh.

4. Then the definition of the method implementation:

A pointer to the function of a method implementation.

#if! Objc_old_dispatch_prototypes

typedef void (*IMP) (void/* ID, SEL, ... */ );

#else

typedef ID (*IMP) (ID, SEL, ...);

#endif

Next, there are some actions that define the name of the method and the variables.

B.runtime.h

Many structures and methods are defined in the Runtime.h file.

1, the first is to give several struct pointer type defined several aliases Objc_method * corresponding method, Objc_ivar * corresponding ivar,objc_catagory * corresponding catagory objc_property * corresponding OBJC_ The property_t type.

2. Objc_class Structural Body

struct Objc_class {

Class Isa objc_isa_availability;

#if!__objc2__

Class Super_class objc2_unavailable;

Const char *name objc2_unavailable;

long version objc2_unavailable;

long info objc2_unavailable;

long instance_size objc2_unavailable;

struct objc_ivar_list *ivars objc2_unavailable;

struct objc_method_list **methodlists objc2_unavailable;

struct objc_cache *cache objc2_unavailable;

struct objc_protocol_list *protocols objc2_unavailable;

#endif

} objc2_unavailable;

From the above structure, you can see a Class-defined ISA member in the Objc_class, a member that can be seen in a as a objc_class * type in fact. (This is defined in the way we learn C + +.) Rectification "Object-oriented C + +". )

Then we need to know that the classes and instances inside the object-c are actually a defined object, and the corresponding pointer is ISA. This means that the system will save the pointer to this ISA after the operation like malloc.

In fact, object-c inside the class and objects are actually through the structure of the pointer to achieve.

Then we look at the structure in Objc_class which includes the parent class, the class name, the version, the sample size, the list of variables contained in the instance, the list of methods, the cache, the protocol list. These things

Looking down you can see that protocol is a objc_object type and then save the corresponding name, method, variable, etc. through the ISA pointer inside the Objc_object. So through this you can realize that defining a protocol is a way to add the attributes you want.

and then down.

Objc_method_description structure This thing defines methods with method names and method types

objc_property_attribute_t This struct defines attributes that include the need for a name and a value.

And then there's a lot of work done on the instance at runtime, and here I'm going to omit it. Another way to call

Imp_implementationwithblock

From here you can get a pointer to the corresponding implementation of a block. (Block is an object)

And then there are the methods that are associated with the object, which are the usual ways to add attributes to a class in a category (you can't add attributes in a category, but you can set the properties for your definition by this method, and the Get method is equivalent to the overridden operation.) Then implement a category of attributes)

Objc_setassociatedobject

Objc_getassociatedobject

Objc_removeassociatedobjects

And then go down again a bunch of structures protocol_list, Objc_catagory, Objc_ivar, Objc_ivar_list, Objc_method, Objc_method_list, Objc_cache, objc_ Module.

These structures correspond to the structure of the categories, methods, etc. that we often use in OC (vaguely speaking, a bit more)

To summarize, that is to say, iOS runtime is a lot of struct type to cache the corresponding structure and at run time through these structural features and corresponding pointers to analyze and do the corresponding operation.

So look inside the structure of the composition is right.

The above is personal understanding

Recommend a pretty detailed (Http://chun.tips/blog/2014/11/06/bao-gen-wen-di-objective[nil]c-runtime (3) [Nil]-xiao-xi-he-category /)

If you want to know more about the specific internal implementation principle, it is necessary to look at the contents of opensource.apple.com, such as: https://opensource.apple.com//source/objc4/objc4-706/runtime/

About the runtime of 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.