Dynamic types (isKindOfClass, isMemberOfClass, id) of Objective-C syntax

Source: Internet
Author: User

The ability of an object to obtain its type at runtime is called introspection. There are multiple methods to achieve this in introspection.
Determine 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.
Let's try these two methods.
1. The new Person class inherits NSObject, and the new Teacher Class inherits the Person class.
1.1 create a Person class
[Cpp]
# Import <Foundation/Foundation. h>
 
@ Interface Person: NSObject
{
NSString * name;
}
-(Void) setName :( NSString *) n;
 
@ End

[Cpp]
# Import "Person. h"
 
@ Implementation Person
-(Void) setName :( NSString *) n
{
Name = n;
}
 
@ End
1.2 create a Teacher Class
[Cpp]
# Import "Person. h"
 
@ Interface Teacher: Person
 
-(Void) teach;
 
@ End
[Cpp]
# Import "Teacher. h"
 
@ Implementation Teacher
-(Void) teach
{
NSLog (@ "I teach mathematics ");
}
@ End
1.3 first, we will experiment with the isMemberOfClass method.
[Cpp]
NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
Person * person = [[Person alloc] init];
Teacher * teacher = [[Teacher alloc] init];

// YES
If ([teacher isMemberOfClass: [Teacher class]) {
NSLog (@ "member of the teacher Teacher class ");
}
// NO
If ([teacher isMemberOfClass: [Person class]) {
NSLog (@ "member of the teacher Person class ");
}
// NO
If ([teacher isMemberOfClass: [NSObject class]) {
NSLog (@ "member of the teacher NSObject class ");
}
[Person release];
[Teacher release];
[Pool release];
Print result:
14:23:07. 965 ObjectiveCTest [2460: f803] member of the teacher Teacher class
Only the first judge is printed. isMemberOfClass determines whether the instance belongs to this category and whether it is related to the parent class.
1.4 isMemberOfClass Method
[Cpp]
NSAID utoreleasepool * pool = [[NSAID utoreleasepool 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 Class or Teacher ");
}
// YES
If ([teacher isKindOfClass: [Person class]) {
NSLog (@ "teacher is a subclass of the Person class or Person ");
}
// YES
If ([teacher isKindOfClass: [NSObject class]) {
NSLog (@ "teacher is a subclass of NSObject class or NSObject ");
}
[Person release];
[Teacher release];
[Pool release];
14:34:17. 315 ObjectiveCTest [2595: f803] teacher is a subclass of the Teacher Class or Teacher
14:34:17. 316 ObjectiveCTest [2595: f803] teacher is a subclass of the Person class or Person
14:34:17. 316 ObjectiveCTest [2595: f803] teacher is a subclass of the NSObject class or NSObject
All three results are printed.
2,
-(BOOL) respondsToSelector: selector determines whether the instance has such a method
+ (BOOL) instancesRespondToSelector: determines whether the class has this method. This method is a class method and cannot be used in class objects.
2.1 use of respondsToSelector
No object creation or release is written here. Refer to the above Code.
[Cpp]
// 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 result:
14:39:49. 853 ObjectiveCTest [2723: f803] teacher responds to setSize: method
14:39:49. 854 ObjectiveCTest [2723: f803] teacher class responds to alloc method
In the middle of the judgment, I wrote a selector at will, of course, no more. RespondsToSelector check class method alloc returns YES
2.2 instancesRespondToSelector
[Cpp]
// 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 result:
[Cpp]
14:52:29. 378 ObjectiveCTest [2961: f803] Teacher instance responds to teach method
14:52:29. 379 ObjectiveCTest [2961: f803] Teacher instance responds to setName: method

3. Objective-C id type

C ++ uses a strong type: the object must comply with its type; otherwise, it cannot be compiled. In Objective-C, the id type is similar to (void *) and can point to any class instance. Without force conversion.
Next let's take a look at the usage,
Change the teach method in the Teacher class
-(Void) teach
{
NSLog (@ "% @ ", name );
}
Then implement and call
[Cpp]
NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
Person * person = [[Person alloc] init];
Teacher * teacher = [[Teacher alloc] init];
 
Id p = person;
Id t = teacher;
[T setName: @ "Miss Zhang"];
[T teach];
 
[Person release];
[Teacher release];
[Pool release];
Print result:
[Cpp] view plaincopy
14:57:55. 905 ObjectiveCTest [3085: f803] instructor Zhang San teaches mathematics


Author: totogo2010

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.