Nsobject analysis (unfinished)
Reprinted please note source http://blog.csdn.net/uxyheaven
The nsobject class in IOS is not open-source, but Runtime is open-source. There is an object-like interface which is similar to nsobject. Next I will analyze nsobject through the object code.
The runtime code is available at http://opensource.apple.com/tarballs/objc4/objc4-493.9.tar.gz. the objectfile is in the <object. h> folder where the file contains obsolete, er.
Attribute Isa
Is a pointer to the class. For details, refer to this article objective-C objc_class introduction.
Method class
The instance method returns the ISA pointer, and the class method returns itself.
The code is implemented as follows:
- class{ return (id)isa; }+ class { return self;}
Superclass
Returns the parent class.
The code is implemented as follows:
+ superclass { return class_getSuperclass((Class)self); }- superclass { return class_getSuperclass(isa); }
The class_getsuperclass method in Runtime is called, and the returned result is ISA-> superclass, and the returned result is self-> superclass.
static class_t *getSuperclass(class_t *cls){ if (!cls) return NULL; return cls->superclass;}
-(Bool) ismemberof: Aclass
- (BOOL)isMemberOf:aClass{ return isa == (Class)aClass;}
Check the code to check whether the ISA of the instance object is the same as the [Class class] passed in. The ISA of the Instance Object is actually pointing to the class of the Instance Object.
-(Bool) iskindof: Aclass
-(Bool) iskindof: Aclass {register class CLS; For (CLS = ISA; CLS = class_getsuperclass (CLS) if (CLS = (class) Aclass) return yes; return no;} // After the class_getsuperclass is expanded, the following static class_t * getsuperclass (class_t * CLs) {If (! CLs) return NULL; return CLS-> superclass ;}
The Code is also well understood. If your ISA is equal to Aclass (the parent class of Aclass, here the loop), yes is returned; otherwise, no is returned.