Several important functions of the iOS reflection mechanism

Source: Internet
Author: User

/*object-c Grammar Study Main.mmmade by davidsu33-(BOOL) iskindofclass:classobj Whether it is a descendant or a member-(BOOL) Ismem Berofclass:classobj whether it is a member-(BOOL) Respondstoselector:selector If there is this method + (bool) Instanc Does the object of the Esrespondtoselector:selector class have this method-(ID) Performselector:selector The method of executing the object */#im Port <foundation/foundation.h>typedef struct{float x,y,w,h;} Rect;typedef struct{float x, y;} Point; @protocol dsshape@optional-(int) Getarea; @optional-(Rect) Getboundingrect; @optional-(nsmutablearray*)    Getpointarray; @end @interface dsrectangle:nsobject<dsshape>{Rect _rect; nsstring* _name;} @property (ReadWrite, nonatomic) rect rect; @property (ReadWrite, nonatomic) nsstring *name;-(id) init;-(void) dealloc;-( nsstring*) classname;-(BOOL) issqure;-(void) showinfo;-(Rect) getboundingrect; @end @implementation Dsrectangle@synthesize rect = _rect, name = _name;-(ID) init{if (self = [super init]) {        Self.rect = rect{0,0,0,0};        Self.name = nil;    NSLog (@ "%@ initialize", [self className]); } return self;} -(void) dealloc{NSLog (@ "%@ dealloc", [self className]);} -(nsstring*) classname{return Nsstringfromclass ([self class]); -(BOOL) issqure{return SELF.RECT.W = = self.rect.h;}                                        -(void) showinfo{id s = [[NSString alloc] initwithformat:@ "x=%f,y=%f,w=%f,h=%f, name=%@", _rect.x,    _rect.y,_rect.w,_rect.h,self.name]; NSLog (@ "Dsrectangle:%@", s);} -(Rect) getboundingrect{return self.rect;} @end @interface dssqure:dsrectangle-(id) init;-(void) dealloc;-(float) sidelength; @end @implementation dssqure-(ID) in    it{if (self = [super init]) {NSLog (@ "%@ initialize", [self className]); } return self;} -(void) dealloc{NSLog (@ "%@ dealloc", [self className]);} -(float) sidelength{return SELF.RECT.W;} @end # define SYNTAXTEST (x) do{NSLog (@ "%@ =%@", [[NSString alloc] initwithcstring: #x], [email protected] "TRUE": @ "FALSE");    }while (0) void Dynamic_attr_test () {Dsrectangle *re = [[Dsrectangle alloc] init];    Re.name = @ "Rectangle";    Re.rect = rect{10,20,200,300};    Dssqure *sq = [[Dssqure alloc] init];    Sq.name = Nsstringfromclass ([sq class]);    Sq.rect = rect{30,40,500,600};    NSLog (@ "Rectangle name:%@", re.name);    [Re showinfo];    [Sq showinfo];    NSLog (@ "\ n-------------------iskindofclass-----------------");    Syntaxtest ([Re iskindofclass: [NSObject class]]);    Syntaxtest ([Re iskindofclass: [Dsrectangle class]]);    Syntaxtest ([Dsrectangle iskindofclass: [NSObject class]]);    Syntaxtest ([Dsrectangle iskindofclass: [Dsrectangle class]]);    The dynamic syntax tests whether the//iskindofclass object is the class, or an instance of the ancestor of that class Syntaxtest ([Re iskindofclass: [NSObject class]]);    Syntaxtest ([Re iskindofclass: [Dsrectangle class]]);    Whether the Ismemberof object is an instance of a class NSLog (@ "\ n-------------------ismemberofclass-----------------"); Syntaxtest ([Re ismemberofclass: [NSObject class]]);    Syntaxtest ([Re ismemberofclass: [Dsrectangle class]]);    Syntaxtest ([Dsrectangle ismemberofclass: [NSObject class]]);    Syntaxtest ([Dsrectangle ismemberofclass: [Dsrectangle class]]); If the instance is an input parameter, then selector is the member function of the class that returns TRUE//if the class name is an input parameter, then selector is the static function of the class returns true, otherwise false NSLog (@ "\    N-------------------respondstoselector-----------------");    Syntaxtest ([Re respondstoselector: @selector (init)]);    Syntaxtest ([Re respondstoselector: @selector (nothat)]);    Syntaxtest ([Re respondstoselector: @selector (Showinfo)]);    Syntaxtest ([Dsrectangle respondstoselector: @selector (init)]);    Syntaxtest ([Dsrectangle respondstoselector: @selector (nothat)]);    Syntaxtest ([Dsrectangle respondstoselector: @selector (Showinfo)]);    View protocol Interface Syntaxtest ([Re respondstoselector: @selector (Getarea)]);    Syntaxtest ([Re respondstoselector: @selector (Getboundingrect)]);    Syntaxtest ([Dsrectangle respondstoselector: @selector (Getarea)]); Syntaxtest ([Dsrectangle respondstoselector: @selector (GetboUndingrect)]);    Determine if the method is an instance interface (class member function) or a class function (static function) NSLog (@ "\ n-------------------instancesrespondtoselector-----------------");    /*syntaxtest ([Re instancesrespondtoselector: @selector (init)]);    Syntaxtest ([Re instancesrespondtoselector: @selector (nothat)]);    Syntaxtest ([Re instancesrespondtoselector: @selector (Showinfo)]);    *///instancesrespondtoselector can only input class, cannot write other syntaxtest ([Dsrectangle instancesrespondtoselector: @selector (init)]);    Syntaxtest ([Dsrectangle instancesrespondtoselector: @selector (nothat)]);    Syntaxtest ([Dsrectangle instancesrespondtoselector: @selector (Showinfo)]); /* 1. Respondstoselector can check either the class (whether it responds to the specified class method) or the instance (whether it responds to the specified instance method) 2. Instancesrespondtoselector can only be written behind the class name, but an instance is detected (whether it responds to the specified instance method) and [Test Instancesrespondtoselector:sel] is considered equivalent to [obj    Respondstoselector:sel] *///[re release]; [sq release];}    int main () {id arp = [[NSAutoreleasePool alloc] init];    Dynamic_attr_test ();    GetChar ();    [ARP drain]; return 0;}

Several important functions of the iOS reflection mechanism

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.