Dynamic types of Objective-c syntax (Iskindofclass, ismemberofclass,id), etc.

Source: Internet
Author: User

Reprinted from: Copyright statement: This article was originally created by http://blog.csdn.net/totogo2010/, welcome to reprint and share. Please respect the author's labor, keep the statement and the author's blog link when reprinting, thank you!

Original link: http://blog.csdn.net/totogo2010/article/details/7714960/#

The ability of an object to obtain its type at runtime is called introspection. Introspection can be achieved in a number of ways.

Judging Object Type

-(BOOL) isKindOfClass: classObj determines whether it is an instance of this class or a subclass of this class

-(BOOL) isMemberOfClass: classObj determines whether it is an instance of this class

 

We try to use these two methods.

1. New Person class inherits NSObject, New Teacher class inherits Person 1.1, New Person class [cpp] view plaincopy
#import <Foundation / Foundation.h>
  
@interface Person: NSObject
{
    NSString * name;
}
-(void) setName: (NSString *) n;
  
@end
 

[cpp] view plaincopy
#import "Person.h"
  
@implementation Person
-(void) setName: (NSString *) n
{
    name = n;
}
  
@end
1.2 New Teacher class [cpp] view plaincopy
#import "Person.h"
  
@interface Teacher: Person
  
-(void) teach;
  
@end
[cpp] view plaincopy
#import "Teacher.h"
  
@implementation Teacher
-(void) teach
{
    NSLog (@ "I teach math");
}
@end
1.3 We first experiment with the isMemberOfClass method. [cpp] view plaincopy
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   Person * person = [[Person alloc] init];
   Teacher * teacher = [[Teacher alloc] init];
     
   // YES
   if ([teacher isMemberOfClass: [Teacher class]]) {
        NSLog (@ "teacher Teacher member");
   }
   // NO
   if ([teacher isMemberOfClass: [Person class]]) {
       NSLog (@ "teacher Person class member");
   }
   // NO
   if ([teacher isMemberOfClass: [NSObject class]]) {
       NSLog (@ "teacher member of NSObject class");
   }
   [person release];
   [teacher release];
   [pool release];
Print results:

2012-07-04 14: 23: 07.965 ObjectiveCTest [2460: f803] Member of teacher Teacher class

Only the first judgement is printed, and isMemberOfClass judges whether it belongs to this class of instance and whether it is related to the parent class. He does not care.

1.4 isKindOfClass method
 

[cpp] view plaincopy
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Person * person = [[Person alloc] init];
Teacher * teacher = [[Teacher alloc] init];
  
// YES
if ([teacher isKindOfClass: [Teacher class]]) {
    NSLog (@ "teacher is a subclass of Teacher or Teacher");
}
// YES
if ([teacher isKindOfClass: [Person class]]) {
    NSLog (@ "teacher is a subclass of Person or Person");
}
// YES
if ([teacher isKindOfClass: [NSObject class]]) {
    NSLog (@ "teacher is an NSObject class or a subclass of NSObject");
}
[person release];
[teacher release];
[pool release];
2012-07-04 14: 34: 17.315 ObjectiveCTest [2595: f803] teacher is a subclass of Teacher or Teacher

2012-07-04 14: 34: 17.316 ObjectiveCTest [2595: f803] teacher is a subclass of Person or Person

2012-07-04 14: 34: 17.316 ObjectiveCTest [2595: f803] teacher is an NSObject class or a subclass of NSObject

All three results are printed.

2,
-(BOOL) respondsToSelector: selector

+ (BOOL) instancesRespondToSelector: Determine whether the class has this method. This method is a class method and cannot be used on objects of the class

2.1 Use of respondsToSelector
The creation and release of objects are not written here, refer to the above code

[cpp] view plaincopy
// YES
if ([teacher respondsToSelector: @selector (setName:)] == YES) {
    NSLog (@ "teacher responds to setSize: method");
}
  
// NO
if ([teacher respondsToSelector: @selector (abcde)] == YES) {
    NSLog (@ "teacher responds to nonExistant method");
}
  
// YES
if ([teacher respondsToSelector: @selector (alloc)] == YES) {
    NSLog (@ "teacher class responds to alloc method \ n");
}
Print results:

2012-07-04 14: 39: 49.853 ObjectiveCTest [2723: f803] teacher responds to setSize: method

2012-07-04 14: 39: 49.854 ObjectiveCTest [2723: f803] teacher class responds to alloc method

The middle judgement I just wrote a selector, of course it is gone. respondsToSelector check class method alloc returns YES

2.2 instancesRespondToSelector
 

[cpp] view plaincopy
// NO
    if ([Person instancesRespondToSelector: @selector (teach)] == YES) {
        NSLog (@ "Person instance responds to teach method");
    }
      
    // YES
    if ([Teacher instancesRespondToSelector: @selector (teach)] == YES) {
        NSLog (@ "Teacher instance responds to teach method");
    }
    // YES
    if ([Teacher instancesRespondToSelector: @selector (setName :)] == YES) {
        NSLog (@ "Teacher instance responds to setName: method");
    }
Print results:

 

[cpp] view plaincopy
2012-07-04 14: 52: 29.378 ObjectiveCTest [2961: f803] Teacher instance responds to teach method
2012-07-04 14: 52: 29.379 ObjectiveCTest [2961: f803] Teacher instance responds to setName: method
 

3.Object-C id type

C ++ uses strong typing: objects must conform to their types, otherwise they won't compile. In Objective-C, the id type is similar to (void *) and can point to an instance of any class. No need to cast.

Let ’s take a look at the use,

First modify the teach method in the Teacher class to

-(void) teach

{

    NSLog (@ "% @ TEACH MATH", name);

}

Then implement and call

[cpp] view plaincopy
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Person * person = [[Person alloc] init];
Teacher * teacher = [[Teacher alloc] init];
  
id p = person;
id t = teacher;
[t setName: @ "张三 老师"];
[t teach];
  
[person release];
[teacher release];
[pool release];
Print results:

[cpp] view plaincopy
2012-07-04 14: 57: 55.905 ObjectiveCTest [3085: f803] Teacher Zhang San teaches mathematics
Objective-C syntax dynamic type (isKindOfClass, isMemberOfClass, id), etc.


Related Article

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.