Preliminary understanding of runtime--Structure and class

Source: Internet
Author: User

Preliminary understanding of Runtime

    • Preliminary understanding of Runtime
      • Runtime Introduction
      • The relationship between class and struct body
      • Structural Body Analysis
      • The role of the structural body

Runtime Introduction

Learn a thing at least first know what it is, you must have heard that "runtime is a feature of the Objective-c", where the "runtime" refers to the runtime.

Runtime is an advanced technology in a programming language that is open from the IOS platform and developed based on the Objective-c language.

The purpose of learning runtime is not to develop, but to allow you to better understand the working principle of objective-c, so as to skillfully use objective-c to develop applications. Of course, you can also use the theory of runtime to add points to yourself during an interview.

Runtime is very powerful, very powerful, very powerful, powerful to let you speechless. Even because of the Runtime, it makes objective-c very different from other object-oriented languages.

Once on the forum to see such a phrase " if there is no OC in the world, a person who can skillfully use the Runtime can write a oc." In fact, Apple is using the runtime to make the present objective-c language. Runtime just inserted the object-oriented wings into the C language, and then he could fly with the object-oriented language ( ̄︶ ̄) ^.

The relationship between class and struct body

Any executable programming language, eventually compiled will become a compilation, but OC to the assembly is not a step, also experienced C, and possibly even C + +.

But C language is closest to assembly language, because C language has direct memory operation. and C language is written in English, people easy to read the language. OC becomes a compilation before it becomes a C, then the assembly.

OC is an object-oriented language, and C language is process-oriented. The biggest difference between OC and C is that C has no "class". So, when OC becomes C, what does object-oriented class become, and how does a class exist in a process-oriented language? when it comes to this problem, the role of runtime is revealed.

Runtime uses a struct (struct) in the process-oriented language (C) to implement the class in object-oriented language (OC). In fact, we can see what this class looks like after it becomes a struct in C.

We can import header files in the file

#import <objc/runtime.h>

Then use the command key and 鼠标左键 enter the runtime's header file definition, you will find a structure like the following

structObjc_class {class ISA objc_isa_availability;#if!__objc2__Class Super_class objc2_unavailable;Const Char*name objc2_unavailable;LongVersion objc2_unavailable;LongInfo objc2_unavailable;LongInstance_size objc2_unavailable;structObjc_ivar_list *ivars objc2_unavailable;structObjc_method_list **methodlists objc2_unavailable;structObjc_cache *cache objc2_unavailable;structObjc_protocol_list *protocols objc2_unavailable;#endif} objc2_unavailable;

The structure of the name is called objc_class , in fact, he is our usual in the OC commonly used Class . When we get here, we wonder how the two of them have been related.

Continue Importing header files

#import <objc/objc.h>

You'll see these lines as you go in.

/// An opaque type that represents an Objective-C class.struct objc_class *Class;/// Represents an instance of a class.struct objc_object {    Class isa  OBJC_ISA_AVAILABILITY;};/// A pointer to an instance of a class.struct objc_object *id;

If you have a C-language foundation, you must know typedef the grammar as well as the meaning

Apple redefined objc_class The structure and named it *Class , that is, a Class pointer to objc_class the other. idthe same is also objc_object a pointer.

Very obviously, usually we write code like this:

id obj = [[NSObject alloc] init];

objis an NSObject object, but essentially a obj id id objc_object Pointer to a struct, so what you actually get is just a pointer to the struct's body .

So again, the class actually has objects, because you can hit it like this:

Classclass];

So C is the type of OBJC, and then you can create the object directly from this C:

id other_obj = [[c alloc] init];

If you are careful, you will find

ClassSave a class object, but the essence is objc_class the pointer

idSave an object, but the essence is objc_object the pointer

And all of the object-oriented programming that we do, the manipulation of objects, will eventually be implemented into these structures. These structures hold all the information about an object.

Structural Body Analysis

objc/objc.hand objc/runtime.h two header files for the contents to make everyone look good:

typedef structObjc_class {class ISA objc_isa_availability;#if!__objc2__Class Super_class objc2_unavailable;Const Char*name objc2_unavailable;LongVersion objc2_unavailable;LongInfo objc2_unavailable;LongInstance_size objc2_unavailable;structObjc_ivar_list *ivars objc2_unavailable;structObjc_method_list **methodlists objc2_unavailable;structObjc_cache *cache objc2_unavailable;structObjc_protocol_list *protocols objc2_unavailable;#endif} *class objc2_unavailable;//Above is a struct of a classtypedef structObjc_object {Class isa objc_isa_availability;} *ID;//Above is a struct of an object

We start with the object structure (objc_object).

When we get to an object, it is a reference to an object, that is, one id . The structure above is redefined as *id id a objc_object pointer.

There is a member in this struct, which is Class a type isa . We can understand that the object that we instantiate, which actually holds only one 类的结构体 pointer, is to illustrate what class the object is instantiated from.

Now look at the structure of the Class (Objc_class).

Once the object is instantiated, he saves the information that creates his own class, and this information is a objc_class struct, so let's see if the struct actually holds the information about a class.

  • Class isa
    • A objc_class pointer to a save, calledisa
    • This and the same name isa objc_object , the specific content and the difference after analysis
  • Class super_class
    • A objc_class pointer to a save, calledsuper_class
    • Obviously, here is a pointer to the parent class, in order to save where you are integrated from
  • char *name
    • A string pointer that holds the class name
    • This doesn't have to be explained, the value in NSObject class is "NSObject".
  • long version
  • long info
  • long instance_size
    • The above three members do not explain much, do not use
  • struct objc_ivar_list *ivars
    • As you can see from the definition, it is a struct pointer that holds the list of instance variables for this class
  • struct objc_method_list **methodLists
    • As you can see from the definition, it is a struct pointer that holds the list of methods for this class
  • struct objc_cache *cache
    • As you can see from the definition, it is a struct pointer that holds the cache
    • What can be saved to the cache, this will explain
  • struct objc_protocol_list *protocols
    • As you can see from the definition, it is a struct pointer that holds the list of protocols

We write classes that are actually these things, some instance variables, some methods, implementations of some protocols, a name for the class, and all the attributes of the parent class.

Now we finally know what kind of struct the class we write will eventually become.

The role of the structural body

A structure is a piece of memory that is used to uniformly save a set of different sizes and invariant.

in the C language, we are allowed to create and modify structs , that is, to dynamically modify classes in OC.

We can dynamically modify the contents of these structures, even after you create an object, you can really modify his type and the parent class. You can even add methods dynamically for a class, rather than writing methods in. m files. Similarly, you can even modify the classes that Apple writes . This is where the runtime is so powerful. First introduced here. An article about runtime applications will be updated recently.

You can keep looking.
The preliminary understanding of runtime--message mechanism
The practice of runtime-adding attributes (associative objects) to a class
The practice of runtime-the exchange of methods

Preliminary understanding of runtime--Structure and class

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.