IOS objective-c Runtime One: Classes and objects

Source: Internet
Author: User

// ---------------------------------------------------

Reference: South Peak son's technical blog Http://southpeak.github.io

//----------------------------------------------------

OC language is a dynamic language that puts a lot of static language in the process of compiling and linking time to the runtime. The advantage of this dynamic language is that we have more flexibility in writing code, such as the ability to forward messages to the object we want, or to exchange the implementation of a method at will.

This feature means that OC needs not only a compiler, but also a runtime system to execute the compiled code, which, for OC, is like an operating system, which allows all the work to run properly. This runtime system is OBJC runtime. OBJC runtime is actually a runtime library, it is basically written with C drink, this library makes C language has the object-oriented ability

The runtime library mainly does the following several things:

1. Encapsulation: In this library, objects can be represented in the struct in C, and methods can be implemented with C functions, plus some additional features. Once these structures and functions are encapsulated by the runtime function, we can create, examine, modify, and alter the classes, objects, and their methods as the program runs.

2. Find out the final execution code of the method: when the program executes [object DoSomething], a message (dosomething) is sent to the message Receiver (object), and the runtime responds differently depending on whether the message receiver can respond to the message. This will be described in more detail later.

There are currently two versions of OC Runtime: Modern runtime and Legacy runtime. Modern runtime covers 64-bit Mac OS X apps, and iOS apps,legacy runtime is used to get up early for 32-bit Mac OS X apps.

The basic working principle of runtime and how to use it to make the program more flexible.

Class and object base data structures

Class

In OC, a class is represented by the class type, which is actually a pointer to the Objc_class struct body. Defined as follows:

typedef struct OBJC_CLAA *class;

See the definition of objc_class struct in objc/runtime.h as follows:

struct Objc_class {
Class Isa objc_isa_availability;

#if!__objc2__

Class Super_class objc2_unavailable; Parent class

const char *name objc2_unavailable; Class name

Long version objc2_unavailable; The version information for the class, which defaults to 0

Long info objc2_unavailable; Class information for some bit identifiers that are used for the run time

Long Instance_size objc2_unavailable; Instance variable size for this class

struct Objc_ivar_list *ivars objc2_unavailable; The list of member variables for this class

struct Objc_method_list **methodlists objc2_unavailable; Method definition Linked List

struct Objc_cache *cache objc2_unavailable; Method Cache

struct Objc_protocol_list *protocols objc2_unavailable; Protocol link List

#endif

}objc2_unavailable;

In this definition, the following fields are of interest to us

1.isa: It should be noted that in OC, all classes themselves are also an object, and the object's class also has an Isa pointer, which points to the Metaclass (Meta Class), which is explained later

2.super class: Points to the parent class of the class, Super_class is null if the class is already the topmost root class (such as NSObject or Nsproxy).

3.cache: Used to cache the most recently used method. When a recipient object receives a message, it looks for an object that responds to the message according to the ISA pointer, and in actual use, only a subset of the methods are commonly used, and many methods are seldom used or used at all, in which case, if each message comes, We all go through the methodlists, performance is bound to be poor, at this time, the cache comes in handy, after we call a method, this method will be cached to the cache list, the next call, runtime will first go to the cache to find, If the cache does not, take the Find method in Methodlists. This makes the invocation more efficient for calls to frequently used methods.

4.version: We can use this field to provide the version information of the class, which is useful for serialization of classes, which allows us to identify changes in the layout of instance variables in different class definition versions, and for the cache, the following example illustrates the execution process "

Nsarray *array = [[Nsarray alloc] init];

Process:

1.[nsarray alloc] execution, because Nsarray no +alloc method, so go to the parent class NSObject find

2. Detects if the NSObject responds with the + Alloc method, finds the response, detects the Nsarray class, and starts allocating memory based on the memory space it needs, and then points the ISA pointer to the Nsarray class. Also, +alloc is added to the cache list

3. Next, execute the-init method and, if the Nsarray response method, add it directly to the cache, or, if it does not, take the parent class lookup.

4. In later operations, if you create an array again using [[Nsarray alloc] init], the appropriate method is removed directly from the cache and called directly

Objc_object and ID

Objc_object is an instance struct that represents a class, which is defined as follows (OBJC/OBJC.H)

struct Objc_object {

Class Isa objc_isa_availability;

};

typedef struct OBJC_OBJCT *id;

As you can see, the struct has only one word, that is, the ISA pointer to its class, so that when we send a message to an OC object, the runtime library finds the class to which the instance object belongs, based on the ISA pointer of the instance object. The runtime library looks for the method that the selector points to for the message in the method list of the class and in the list of methods of the parent class. Run this method when you find it.

When you create an instance object of a particular class, the allocated memory contains a OBJC_OBJECT data structure, and then a tired amount of the instance variable. The Alloc and Allocwithzone of the NSObject class: Methods use the function class_createinstance to create OBJC_OBJECT data structures.

There is also our common ID, which is a pointer to a objc_object struct type. Its presence allows us to implement operations similar to those of the C + + generics, which can be converted to any object, somewhat similar to the role of the void* pointer type in C.

IOS objective-c Runtime One: Classes and objects

Related Article

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.