Objective-C objc_class Introduction

Source: Internet
Author: User

Objective-C objc_class Introduction
Objective-C objc_class introduction NSObject

In Objective-C, NSObject is the root class of most classes.

@interface NSObject 
  
    {    Class isa  OBJC_ISA_AVAILABILITY;}
  

It has an isa attribute and its type is Class.

Apple has made the ObjC runtime code open source, we download to see what Class is http://opensource.apple.com/tarballs/objc4/objc4-493.9.tar.gz

Objc_class

We can The definition of the Class is displayed in the file.

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

Class is a pointer of the objc_class structure type, and id is a pointer of the objc_object structure type.

The definition of objc_class can be found in Find

struct objc_class {    Class isa;    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;} OBJC2_UNAVAILABLE;

Next, let's look at the meaning of each attribute.

Isa

Is an Objective-C Class pointer. the instance object has an isa attribute pointing to Class, and the Class also has an isa attribute pointing to meteClass. here is a point. In Objective-C, any class definition is an object.

Super_class

Point to the parent class of the class. If the class is already the top-level root class (such as NSObject or NSProxy), super_class is NULL.

What about their relationship? There's a graph http://www.sealiesoftware.com/blog/class%20diagram.pdf.


Name

Let's take a look at the following code.

id objc_getClass(const char *aClassName){    if (!aClassName) return Nil;    // NO unconnected, YES class handler    return look_up_class(aClassName, NO, YES);}PRIVATE_EXTERN id look_up_class(const char *aClassName, BOOL includeUnconnected, BOOL includeClassHandler){    BOOL includeClassLoader = YES; // class loader cannot be skipped    id result = nil;    struct old_class query;    query.name = aClassName; retry:    if (!result  &&  class_hash) {        // Check ordinary classes        mutex_lock (&classLock);        result = (id)NXHashGet(class_hash, &query);        mutex_unlock (&classLock);    }    if (!result  &&  includeUnconnected  &&  unconnected_class_hash) {        // Check not-yet-connected classes        mutex_lock(&classLock);        result = (id)NXHashGet(unconnected_class_hash, &query);        mutex_unlock(&classLock);    }    if (!result  &&  includeClassLoader  &&  _objc_classLoader) {        // Try class loader callback        if ((*_objc_classLoader)(aClassName)) {            // Re-try lookup without class loader            includeClassLoader = NO;            goto retry;        }    }    if (!result  &&  includeClassHandler  &&  objc_classHandler) {        // Try class handler callback        if ((*objc_classHandler)(aClassName)) {            // Re-try lookup without class handler or class loader            includeClassLoader = NO;            includeClassHandler = NO;            goto retry;        }    }    return result;}struct old_class {    struct old_class *isa;    struct old_class *super_class;    const char *name;    long version;    long info;    long instance_size;    struct old_ivar_list *ivars;    struct old_method_list **methodLists;    Cache cache;    struct old_protocol_list *protocols;    // CLS_EXT only    const uint8_t *ivar_layout;    struct old_class_ext *ext;};

Objc_getClass returns a class from a string, look_up_class. First, create an old_class object, and assign the value of name to this string. If yes, return old_class. after reading the structure of old_class, we can see that the value stored in the name attribute is the name of the class (in fact, name is not the name)

Version

Class version information. The default value is 0.

Info

Some bitwise identifiers used during runtime.

Instance_size

Instance variable size of this class

Ivars
struct objc_ivar_list {    int ivar_count;    /* variable length structure */    struct objc_ivar ivar_list[1];}

Array of member variables

MethodLists

Method definition array

struct objc_method_list {    struct objc_method_list *obsolete;    int method_count;    /* variable length structure */    struct objc_method method_list[1];}
Objc_cache

Point to the recently used method. Used for method call optimization.

struct objc_cache {    unsigned int mask /* total = mask + 1 */;    unsigned int occupied;    Method buckets[1];};
Protocols

Protocol Array

struct objc_protocol_list {    struct objc_protocol_list *next;    long count;    Protocol *list[1];};

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.