objective c - Category on NSObject also works on Class -


so have category on nsobject called customcategory, following:

#import <foundation/foundation.h>  @interface nsobject (customcategory)  -(bool)dosomething;  @end  #import "nsobject+customcategory.h"  @implementation nsobject (customcategory)  -(bool)dosomething {     nslog(@"done");     return no; }  @end 

ideally, work on object this:

nsobject* object = [nsobject new]; [object dosomething]; 

however, found works in way no problem:

[nsobject dosomething]; 

so wondering, since instance method have added through category, why works on class?

instance methods on nsobject class methods on nsobject.

this works because of way objective-c dynamic dispatch. if send message object method implementation looked in objects class. if send message class sending regular message class object. there implementation looked in called meta-class. meta-classes automatically generated compiler. class methods instance methods of meta-class. handled transparently compiler.

inheritance works on meta-class level. meta-class class inherits meta-class of superclass. have 2 parallel inheritance hierarchies there. root classes nsobject handled differently. there meta-class can't inherit meta-class of superclass there no superclass. root classes meta-class inherits root class itself.

and since class methods of class instance methods of meta-class , nsobjects meta-class inherits nsobject instance methods on nsobject class methods on nsobject.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -