Nsobject analysis (unfinished)

Source: Internet
Author: User
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.

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.