Dynamic Types in Objective-C
Objective-C provides many methods to work with
dynamic types. With these methods programmer can check that object is of the
given class or not, given method is member of the given class or not etc. Table given below describe some of these methods and their brief
description.....
Method | Description |
-(BOOL) isKindOfClass: classObj | is object a descendent or member of classObj |
-(BOOL) isMemberOfClass: classObj | is object a member of classObj |
-(BOOL) respondsToSelector: selector | does the object have a method named specifiec by the selector |
+(BOOL) instancesRespondToSelector: selector | does an object created by this class have the ability to respond to the specified selector |
-(id) performSelector: selector | invoke the specified selector on the object |
Example:
FirstClass.h
FirstClass.m
#import<Foundation/NSObject.h> @interface FirstClass:NSObject -(void)fShow ; +(void)classShow ; @end
|
#import "FirstClass.h" @implementation FirstClass -(void)fShow { printf("This is first class."); } +(void)classShow { } @end |
SecondClass.h
SecondClass.m
#import<Foundation/ NSObject.h> @interface SecondClass: NSObject -(void)sShow; @end |
#import "SecondClass.h" @implementation SecondClass -(void)sShow { printf("This is first class."); } @end |
main.m
#import "FirstClass.m" #import "SecondClass.m" #import <stdio.h> int main() { FirstClass *fClassObj = [[FirstClass alloc] init]; SecondClass *sClassObj = [[SecondClass alloc] init]; /* some methods to work with dynamic types */ // -(BOOL) isKindOfClass: classObj ----------- true if ( [fClassObj isKindOfClass: [FirstClass class]] == YES ) { printf( "fClassObj is kind of FirstClass.\n" ); } // -(BOOL) isKindOfClass: classObj ----------- false if ( [fClassObj isKindOfClass: [SecondClass class]] == YES ) { printf( "fClassObj is kind of SecondClass.\n" ); } else printf( "fClassObj is not kind of SecondClass.\n" ); // -(BOOL) isMemberOfClass: classObj ----------- true if ( [fClassObj isMemberOfClass: [FirstClass class]] == YES ) { printf( "fClassObj is member of FirstClass.\n" ); } // -(BOOL) isMemberOfClass: classObj ----------- false if ( [sClassObj isMemberOfClass: [FirstClass class]] == YES ) { printf( "sClassObj is member of FirstClass.\n" ); } else printf( "sClassObj is not member of FirstClass.\n" ); // -(BOOL) respondsToSelector: selector ----- true if ( [fClassObj respondsToSelector: @selector(fShow)] == YES ) { printf( "fClassObj responds to fShow method\n" ); } // -(BOOL) respondsToSelector: selector ----- false if ( [sClassObj respondsToSelector: @selector(fShow)] == YES ) { printf( "sClassObj responds to fShow method\n" ); } else printf( "sClassObj does'nt respond to fShow method\n" ); // release memory allocated for the objects [fClassObj release]; [sClassObj release]; return 0; } |
Output:
fClassObj is kind of FirstClass. fClassObj is not kind of SecondClass. fClassObj is member of FirstClass. sClassObj is not member of FirstClass. fClassObj responds to fShow method sClassObj does'nt respond to fShow method |