iOS development: Cocoa Classes and objects

Source: Internet
Author: User
Tags class definition data structures instance method static class

We can find the definition of class and object in/usr/include/objc/objc.h and runtime.h:

typedef struct OBJC_CLASS *class;

typedef struct OBJC_OBJECT {

Class Isa;

} *id;

Class is a pointer to a objc_class struct type, and an ID (any object) is a pointer to a objc_object struct type whose first member is a pointer to a objc_class struct type. Note that there is a key explanation: the memory layout with a objc_class pointer for all the things that start with can be treated as an object! What kind of structure is objc_class? and look:

struct Objc_class

{

struct objc_class* Isa;

struct objc_class* super_class;

const char* name;

Long version;

Long info;

Long instance_size;

struct objc_ivar_list* ivars;

struct objc_method_list** methodlists;

struct objc_cache* cache;

struct objc_protocol_list* protocols;

};

The members of the OBJC_CLASS structure are described as follows:

ISA: is a objc_class type of pointer, see here, think of me before the extended interpretation of the? Memory layout all the things that start with a objc_class pointer can be treated as an object! This means that objc_class or class can also be treated as a Objc_object Object! Object is object, class is object, is it a bit confusing? Don't worry, OBJC invented (or reused) a term to distinguish between these two different objects: class objects Object) With instance objects (instance). OK, name confusion problem resolution, I will use these two terms to distinguish between different objects, and use the term "object" to refer to all objects. OBJC also gives a different name to the class structure of the class object and the ISA in the instance object: The ISA in the class object is referred to as the static class member variable of the Metaclass,metaclass storage class and the static class member method (+ Start method); The ISA-pointing class structure in an instance object is called Class (normal), and the class structure stores the normal member variables of the class with the normal member method (-the method at the beginning).

Super_class: A look is clear, pointing to the class of the parent chant! If the class is already the topmost root class (such as NSObject or Nsproxy), then Super_class is null.

OK, let's break down the other class structure members, so that we can clarify the relationship between the subclass, the parent class, the root class (these are normal class) and their corresponding Metaclass Isa and super_class in the inheritance hierarchy:

Rule one: The Isa of the instance object of the class points to the class; The Isa of that class points to the metaclass of the class;

Rule two: The Super_class of a class points to its parent class, or NULL if the class is the root class;

Rule three: Metaclass Isa points to the root metaclass, if the metaclass is the root metaclass points to itself;

Rule four: The super_class of metaclass points to the parent metaclass, and if the metaclass is the root metaclass, point to the corresponding class of the metaclass;

Well, the text is always so weak, there is a picture of the truth!

So what's the difference between class and Metaclass?

Class is the type of the instance object. When we send a message to an instance object (an instance method), we look for the function of the response in the methodlists of the class structure of the instance object, and if no matching response function is found, the methodlists in the parent class of the class finds the row (lookup chain as the middle of the diagram above). )。 As in the following code, sending a lowercasestring message to a STR instance object finds the LowerCaseString response function in the methodlists of the NSString class structure.

NSString * STR;

[Str lowercasestring];

Metaclass is the class type object. When we send a message (class method) to a class object, we look for the response function in the methodlists of the metaclass structure of the class object, and if no matching response function is found, the methodlists in the parent class of the Metaclass finds ( The lookup chain is the rightmost row of the above figure. Sending a stringwithstring message to the NSString class object, as in the following code, finds the Methodlists response function in the stringwithstring of the NSString Metaclass class structure.

[NSString stringwithstring:@ "str"];

OK, so now that we understand the hierarchy of classes, let's look at the other members in the class structure.

Name: A C string that indicates the name of the class. We can find the class by this name (via: ID objc_getclass (const char *aclassname) or the metaclass of the class (the const char * in runtime). Aclassname));

Version: The versioning information for the class, which is initialized to 0 by default. We can modify it (class_setversion) or get (class_getversion) at run time.

Info: Some bit identities for runtime use. Some bit masks are as follows:

Cls_class (0x1l) indicates that the class is normal class, which contains instance methods and variables;

Cls_meta (0x2l) indicates that the class is Metaclass and contains class methods;

Cls_initialized (0x4l) indicates that the class has been initialized by the runtime, and that the identity bit is set only by the Objc_addclass;

Cls_posing (0x8l) indicates that the class is pose into other classes; (Poseclass is discarded in OBJC 2.0);

Cls_mapped (0x10l) is used for the OBJC runtime

Cls_flush_cache (0x20l) is used for the OBJC runtime

Cls_grow_cache (0x40l) is used for the OBJC runtime

Cls_need_bind (0x80l) is used for the OBJC runtime

Cls_method_array (0x100l) indicates whether the methodlists is pointing to a objc_method_list or an array containing a objc_method_list pointer;

Instance_size: The size of the instance variable of the class (including instance variables inherited from the parent class);

Ivars: A pointer to a objc_ivar_list that stores the memory address of each instance variable, or NULL if the class has no instance variables;

Methodlists: In relation to some of the flags of info, the Cls_method_array identification bit determines what it points to (whether it points to a single objc_method_list or an array of objc_method_list pointers), if info sets the Cls_class then objc_method_list the stored instance method, if the Cls_meta is set to store the class method;

Cache: Pointer to Objc_cache, used to cache recently used methods to improve efficiency;

Protocols: A pointer to a objc_protocol_list that stores the formal agreement that the class declaration is to comply with.

Summarize:

OBJC generates two objc_class for each class definition, one that is normal class and the other is metaclass. We can create these two objc_class data structures at run time, and then use Objc_addclass to dynamically create new class definitions.

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.