iOS dynamic type Iskindofclass, Ismemberofclass

Source: Internet
Author: User

The ability of an object to acquire its type at run time is called introspection. Introspection can be accomplished in a number of ways.

Judging object Types

-(BOOL) Iskindofclass:classobj to determine if this class or subclass of this class is an instance of

-(BOOL) ismemberofclass:classobj to determine if it is an instance of this class

Let's try the use of these two methods.

1. New Person class inherits NSObject, new teacher class inherits person
1.1. New Person class
1. #import <Foundation/Foundation.h>
2.
[email protected] Person:nsobject
6.9
5. NSString *name;
6.}
7.-(void) SetName: (nsstring*) n; 8.
[email protected]


 
1. #import "Person.h" &NBSP;&NBSP;&NBSP;
2.  
[email protected]  person  
4.-(void) SetName: (NSString *) n  5. {  
6.    name = n;  
7.}   
8.  
[email  Protected]  

1.2 New Teacher Class
1. #import "Person.h"
2.
[email protected] Teacher:person
4.
5.-(void) teach; 6.
[email protected]

1. #import "Teacher.h"
2.
[email protected] Teacher
4.-(void) Teach 5. {
6. NSLog (@ "I teach maths"); 9.3
[email protected]

1.3 We first experiment with the Ismemberofclass method.
1.NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2. Person *person = [[Person alloc] init];
3. Teacher *teacher = [[Teacher alloc] init];
4.
5.//yes 6.        if ([Teacher Ismemberofclass:[teacher class]]) {7.  NSLog (@ "Member of teacher Teacher Class"); 8.}
9.//no 10.       if ([Teacher Ismemberofclass:[person class]]) {11.  NSLog (@ "Member of teacher Person class"); 12.}
//no 14.       if ([Teacher Ismemberofclass:[nsobject class]]) {15.  NSLog (@ "Member of Teacher NSObject class"); 16.}
[Person release];
[Teacher release];
[Pool release];


Printing results:

2012-07-04 14:23:07.965 objectivectest[2460:f803] Teacher Teacher class members

Only the first judgment is printed, and Ismemberofclass determines whether it belongs to an instance of this class, whether it is related to the parent class or not.

1.4 Ismemberofclass Method
1.NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2.Person *person = [[Person alloc] init];
3.Teacher *teacher = [[Teacher alloc] init];
4.
5.//yes 6.if ([Teacher Iskindofclass:[teacher class]]) {7.  NSLog (@ "Teacher is a subclass of teacher class or teacher"); 8.}
9.//yes 10.if ([Teacher Iskindofclass:[person class]]) {11.  NSLog (@ "Teacher is a subclass of person class or person"); 12.}
13.//yes 14.if ([Teacher Iskindofclass:[nsobject class]]) {15.  NSLog (@ "Teacher is a subclass of NSObject class or nsobject"); 16.}
17.[person release];
18.[teacher release];
19.[pool release];

2012-07-04 14:34:17.315 objectivectest[2595:f803] teacher is a subclass of teacher class or teacher

2012-07-04 14:34:17.316 objectivectest[2595:f803] teacher is a subclass of the person class or person

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

The three results are printed out.

iOS dynamic type Iskindofclass, Ismemberofclass

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.