In-depth introduction to cocoa and objects

Source: Internet
Author: User
Recently, I plan to write something that is relatively low-level in objc, especially related to runtime. Apple has made the objc runtime code open source, we can from: http://opensource.apple.com/source/objc4/objc4-493.9/runtime/browse the source code, or click here to download the source code. Where can I start? That is of course the most basic class and object. Compared with C ++, the class and Object Structures in objc are much more concise and consistent (refer to "Deep Exploration C ++ object model", you will know the complexity of the class and object structure in C ++ ). This article will explain in detail the structure of classes and objects in objc. Next, we will explain how to operate classes at runtime. We can go to/usr/include/objc. H and runtime. h finds the definition of Class and object: typedef struct objc_class * class; typedef struct objc_object {class ISA;} * ID; class is a pointer of the objc_class structure type; the ID (any object) is a pointer of the objc_object structure type, and 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 my previous 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 ISA in the class object and 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 corresponding to metaclass: Rule 1: ISA of the class instance object points to this class; ISA of this class points to metaclass of this class; Rule 2: class super_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 the metaclass is the root metaclass, it points to itself. Rule 4: metaclass super_class 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 is a picture of the truth! <Relationship between ISA and super_class of instance object, class, and metaclass> 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 the 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 structure level 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: class version. 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, including the instance method and variable; cls_meta (0x2l) indicates that the class is metaclass, including the class method; cls_initialized (0x4l) indicates that the class has been initialized during the runtime. This flag is only set by objc_addclass; cls_posing (0x8l) indicates that the class is pose into other classes; (poseclass is deprecated in objc 2.0); cls_mapped (0x10l) indicates that cls_flush_cache (0x20l) is used during the objc runtime and cls_grow_cache (0x40l) is used during the objc runtime) for the objc runtime cls_need_bind (0x80l) is used for the objc runtime cls_method_array (0x100l) this flag indicates that methodlists is directed to Objc_method_list is 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, the memory address that stores each instance variable. If this class does not have any instance variable, it is null. methodlists: it is related to some flag bits of info, the cls_method_array identifier determines whether it points to something (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 method is used to store the instance. If cls_meta is set; cache: pointer to objc_cache, used to cache recently used methods to improve efficiency; Protocols: pointing to objc_protoco Rochelle list pointer, which stores the formal protocol to be followed by the class declaration. Conclusion: 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.

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.