Http://foredoomed.org/blog/2014/02/24/object-modeling-of-objective-c/
Objective-C is an object-oriented programming language formed by the Smalltalk message mechanism added to C. It is mainly used by Apple to develop Mac OS X and iOS operating systems. Since objective-C is an object-oriented programming language, I am interested in how the object is organized and represented in the memory, and how the message mechanism is implemented.
0. nsobject
The nsobject class is similar to the object class in Java. It is the parent class of all classes, that is, the root class. So what kind of class is nsobject. Open the nsobject. h header file to view the nsobject source code:
@interface NSObject <NSObject>{ Class isa;}
We can see that nsobject implements the nsobject protocol interface, which only contains the ISA attribute of the class type. ISA means "is a", and the connection is "is a class". That is to say, this attribute stores information about the class. Let's take a look at the class source code. It is defined in the objc. h header file:
typedef struct objc_class *Class;
Class is objcClass type, objcClass is defined in objc-class.h:
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; #if defined(Release3CompatibilityBuild) struct objc_method_list *methods;#else struct objc_method_list **methodLists;#endif struct objc_cache *cache; struct objc_protocol_list *protocols;};
We can see thatobj_class
It is a struct that contains information about all classes required for running, including the parent class of the class, instance variables, methods, protocols, and so on. Interestingly,obj_class
There is also an ISA attribute, so where does it point again? It points to an object called metaclass, And the type is alsoobj_class
. Therefore, instantiating a class has two objects: itself and metaclass. The purpose is to save the information of the instance method to its own class and save the class method to the metaclass class. So where does ISA in metaclass point? Because the metaclass class does not have the metaclass method, there is no need for another class to save the method information of the metaclass class. Therefore, the isa of the metaclass object points to itself to form a closed loop structure.
1. Message Mechanism
In objective-C, method calling is a little different from other object-oriented languages (such as Java. Method calls in Java can be written as follows:
object.method(argument);
However, in objective-C, write as follows:
[object method:argument];
The difference between the two is that Java method calls directly call the method of the instance object, while objective-C is to send a message. The message sending target is unknown during compilation, but determined at runtime. The method is determined by selector or Sel, that is, the string representing the method name. The receiving object of the message cannot guarantee that the result will be returned. In this case, an exception is thrown.
The compiler sends the message statement
[receiver message]
Convert:
objc_msgSend(receiver, selector, arg1, arg2, ...)
In the objc_msgsend method, the method to be called is found through the aggreger and selector. The type of this method is imp type, and then the method can be executed and the return value is returned. The IMP type here is the C language implementation of the method to be called, that is, a C function pointer.
2. ID
ID is defined in objc. h:
typedef struct objc_object { Class isa;} *id;
We can see that ID is the objc_object structure, which contains a description of the ISA pointer pointing to the class, so that ID can be used to dynamically describe the class type.
ID is similar to VaR in Javascript. That is to say, variables declared with the ID keyword do not know the specific type during compilation, but are determined at runtime. Because of the existence of the ID keyword, objective-C is not simply an object-oriented language, but a mixture of object-oriented language and Dynamic Language, from this point of view, objective-C is a bit like C.
References
[1] Objective-C Wiki
[2] concepts in objective-C Programming: Object Modeling
[3] Objective-C runtime programming guide