IOS development: Cocoa-Explanation of classes and objects

Source: Internet
Author: User

IOS development: Cocoa-Explanation of classes and objects

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

typedef struct objc_class *Class;typedef struct objc_object {    Class isa;} *id;

Class is a pointer of the objc_class structure type, and id (any object) is a pointer of the objc_object structure type. Its first member is a pointer of the objc_class structure type. Note that there is a key extended explanation: the memory layout starts with an objc_class pointer and can be treated as an object! So what kind of structure is objc_class? See:

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 struct are described as follows:

Isa: A pointer of the objc_class type. do you think of the extended explanation? The memory layout starts with an objc_class pointer and can be treated as an object! This means that the objc_class or class can also be treated as an objc_object object! The object is an object, and the class is also an object. Is it a bit confusing? Don't worry, ObjC invented (or reused) a term to distinguish these two different objects: class object and instance object ). OK. Solve the Problem of name confusion. I will use these two terms to distinguish different objects, and use the term "object" to refer to all objects. ObjC also named the class structure pointed to by the class object and the isa in the instance object differently: the isa pointing class structure in the class object is called metaclass, metaclass static class member variables of the storage class and static class member methods (Methods Starting with +); isa in the instance object points to the class structure called class (common ), the class structure stores the common member variables of the class and the common member methods (Methods Starting ).

Super_class: the parent class pointing to this class! If the class is already the top-level root class (such as NSObject or NSProxy), super_class is NULL.

Well, let's take a look at the introduction of other class structure members. Let's clarify that at the inheritance level, sub-classes, parent classes, and root classes (these are common classes) and the relationship between isa and super_class of metaclass:

Rule 1: isa of a class instance object points to this class; isa of this class points to metaclass of this class;

Rule 2: The super_class of the class points to its parent class. If the class is the root class, the value is NULL;

Rule 3: isa of metaclass points to the root metaclass. If this metaclass is the root metaclass, it points to itself;

Rule 4: The super_class of metaclass points to the parent metaclass. If the metaclass is the root metaclass, it points to the corresponding class of the metaclass;

Well, the text is always so weak that there are pictures and truth!

<Relationship between isa and super_class of instance object, class, and metaclass>

So what is the difference between class and metaclass?

Class is the class type of instance object. When we send a message (instance method) to an instance object, we find the Response Function in the methodlists of the class structure of the Instance Object, if no matching response function is found, search for it in methodlists of the class's parent class (find the row in the middle of the chain ). In the following code, send the lowercaseString message to the str instance object, and search for the response function of lowercaseString in the methodlists of the NSString class structure.

NSString * str;[str lowercaseString];

Metaclass is the class type of class object. When we send a message (Class Method) to a class object, we find the Response Function in the methodlists of the metaclass structure of the Class Object, if no matching response function is found, search for the methodlists in the parent class of the metaclass (the rightmost row of the link ). In the following code, the stringWithString message is sent to the NSString class object. The stringWithString response function is searched in the methodlists of the metaclass class structure of NSString.

[NSString stringWithString:@"str"];

Well, now we understand the structural hierarchy of the class. Let's look at other members in the class structure.

Name: a c string indicating the name of the class. We can find the class (via: id objc_getClass (const char * aClassName) or metaclass (id objc_getMetaClass (const char * aClassName) through this name at runtime ));

Version: the version of the class. The default value is 0. We can modify the class_setVersion or obtain it (class_getVersion) at runtime ).

Info: Bit IDs used during runtime. There are some bit masks:

CLS_CLASS (0x1L) indicates that the class is a common class, which contains instance methods and variables;

CLS_META (0x2L) indicates that the class is metaclass, which contains class methods;

CLS_INITIALIZED (0x4L) indicates that the class has been initialized during the runtime, and this flag is only set by objc_addClass;

CLS_POSING (0x8L) indicates that the class is pose into another class; (poseclass is discarded in ObjC 2.0 );

CLS_MAPPED (0x10L) is used during the ObjC runtime.

CLS_FLUSH_CACHE (0x20L) is used during the ObjC runtime.

CLS_GROW_CACHE (0x40L) is used during the ObjC runtime.

CLS_NEED_BIND (0x80L) is used during the ObjC runtime.

CLS_METHOD_ARRAY (0x100L) indicates whether methodlists points to an objc_method_list or an array containing the objc_method_list pointer;

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

Ivars: pointer to objc_ivar_list to store the memory address of each instance variable. If this class does not have any instance variable, it is NULL;

MethodLists: it is related to some flag spaces of info. The CLS_METHOD_ARRAY identifier determines whether it points to a single objc_method_list or an objc_method_list pointer array. If CLS_CLASS is set in info, The objc_method_list storage instance method is used, if CLS_META is set, the storage class method is used;

Cache: pointer to objc_cache, used to cache recently used methods to improve efficiency;

Protocols: pointer to objc_protocol_list, which stores the formal protocol to be followed by such declarations.

Summary:

ObjC generates two objc_classes for each class definition. One is a common class and the other is metaclass. We can create the two objc_class data structures at runtime, and then use objc_addClass to dynamically create a new class definition. Is this dynamic enough? Next, we will show you how to dynamically create new classes at runtime.

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.