OBJECTC's Isa explanation

Source: Internet
Author: User

Each Objective-c object has a hidden data structure, which is the first member variable of the Objective-c object, which is the ISA pointer.

Inside the NSObject.h:

@interface NSObject <nsobject> {

Class Isa objc_isa_availability;

}

Open the definition of Class:

struct Objc_class {

Class Isa objc_isa_availability;

#if!__objc2__

Class Super_class objc2_unavailable;

Const char *name objc2_unavailable;

long version objc2_unavailable;

long info objc2_unavailable;

long instance_size objc2_unavailable;

struct objc_ivar_list *ivars objc2_unavailable;

struct objc_method_list **methodlists objc2_unavailable;

struct objc_cache *cache objc2_unavailable;

struct objc_protocol_list *protocols objc2_unavailable;

#endif

}

These definitions for the understanding of people naturally understand, no one can not understand. It is recommended to read the following example, and then click on the links inside the link to read it over.

What we now know is that for one of our newly created classes, there will be a hidden property of Isa, which can be accessed through it.

We now create a new class classparent, inherited from NSObject, which has member Method-(void) Selectorp, class method + (void) Classselectorp.

Then create a new class Classchild, inherited from the Classparent, which has the member Method-(void) Selectorc, class method + (void) Classselectorc.

Now we create a new instance classchild* child = [Classchlid new];

When we call [child class], the child will use the ISA pointer to find her own class object.

When we call [child superclass], the child will go through Super_class to find the Classparent class object.

Here, the two types of objc_class are popularized:

The class instance object (child) of the structure that the ISA points to;

Metaclass class object (child, note uppercase) of a struct to which Isa points;

Next, call [child selectorc],child to find the class of the instance object through ISA, find Selectorc in the method List of class (notice the definition of the struct objc_class above);

Try calling [child selectorp],child to find the class of the instance object through ISA, and find that the class does not have this method, through the class inside the Super_class find Classparent class, The list of methods in the inside found the Selectorp;

Again is the class method [child classselectorc],child (note that this is a class object) to find the metaclass of the class object through ISA (actually class, the type is the same, just to differentiate and call the different name), The CLASSSELECTORC is found in the Metaclass method list of the class object.

Try calling [child classselectorp],child to find the metaclass of the class object through ISA, and find that metaclass inside does not have this method, through Metaclass inside the Super_ Class found Classparent Metaclass, in the list of methods found Classselectorp;

Here are a few examples that have basically covered most of the invocation scenarios.

Finally, we summarize the following:

Child is an instance object, there can be more than one, and Isa points to a class that has multiple;

Child is a class object with only one, and Isa points to only one metaclass.

Finally, in order to understand the role of class and Metaclass, we can think about, if we as the runtime's designer, when the developer creates an instance object child, we should store the child's properties and methods, but also how to respond to its method calls, Finally, you record the inheritance relationship with the parent.

Please note that the child is actually just

Reference

Http://www.cocoachina.com/ios/20141018/9960.html

http://blog.csdn.net/jasonblog/article/details/7246822

http://blog.csdn.net/totogo2010/article/details/8081253

OBJECTC's Isa explanation

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.