- Each instance has a pointer to a class object to indicate its type, and these class objects form the inheritance system of the class.
- If the object type cannot be determined at compile time, then the type information query method should be used to detect it.
- Try to use Type information query methods to determine object types, rather than directly comparing class objects, because some objects may implement message forwarding.
The type ID can refer to any Objective-c object type, and the compiler assumes that an object of type ID can respond to all messages
The nature of Objective-c objects
Each Objective-c object instance is a pointer to a block of memory data. , so when declaring a variable, the type is followed by a "*" character
@" Some String ";
The ID itself is already a pointer
ID @" Some String ";
The data structure used to describe the Objective-c object is defined in the header file of the run-time library.typedef struct OBJC_OBJECT [Class Isa;} *id;
typedef struct OBJC_CLASS *class;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;};
This struct stores the "metadata" (metadata) of the class. The type that the class object belongs to, that is, the type that the ISA pointer points to, called a "meta-class" (Metaclass). Used to describe the metadata of the class object itself.
"Ismemberofclass:" To determine whether an object is an instance of a particular class. "Iskindofclass:" To determine whether an object is an instance of a class or its derived class.
Compares class objects for equality. generally use the = = operator instead of "IsEqual:". Because class objects are "singleton," within an application scope, each class has only one instance.
However, it is best to use the type information query method. You should not directly compare two class objects to be equal. Because holding can correctly handle those objects that use the message passing mechanism. For example, an object might forward all the selected children it receives to another object. Such objects are called "proxies", which all use Nsproxy as the root class. Typically, the class method is called on this type of proxy object, and the proxy object itself is returned. The class to which the object belongs, rather than the accepted proxy. If you use the type information query method, the proxy object will pass this message to the "Object accepting the proxy".
Effective OBJECTIVE-C 2.0-14th: Understanding the purpose of "class objects"