Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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


Download Source Code

                         

» View all related tutorials
Related Tags: iphone c programming mac application import object language io sed system ip port applications this app for phone program to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.