Document directory
- Dynamic types (iskindofclass, ismemberofclass, ID) of objective-C syntax
Dynamic types (iskindofclass, ismemberofclass, ID) of objective-C syntax
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, the new teacher Class inherits person1.1, and the new person class.
- # Import <Foundation/Foundation. h>
-
- @ Interface person: nsobject
- {
- Nsstring * Name;
- }
- -(Void) Setname :( nsstring *) N;
-
- @ End
- # Import "person. H"
-
- @ Implementation person
- -(Void) Setname :( nsstring *) N
- {
- Name = N;
- }
-
- @ End
1.2 create a teacher Class
- # Import "person. H"
-
- @ Interface Teacher: person
-
- -(Void) Teach;
-
- @ End
- # Import "teacher. H"
-
- @ Implementation teacher
- -(Void) Teach
- {
- Nslog (@ "I teach mathematics ");
- }
- @ End
1.3 first, we will experiment with the ismemberofclass method.
- NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
- Person * person = [[person alloc] init];
- Teacher * teacher = [[teacher alloc] init];
-
- // Yes
- If([Teacher ismemberofclass: [teacherClass]) {
- Nslog (@ "member of the Teacher class ");
- }
- // No
- If([Teacher ismemberofclass: [PersonClass]) {
- Nslog (@ "member of the Teacher Person class ");
- }
- // No
- If([Teacher ismemberofclass: [nsobjectClass]) {
- Nslog (@ "member of the teacher nsobject class ");
- }
- [Person release];
- [Teacher release];
- [Pool release];
Print result:
14:23:07. 965 objectivectest [2460: f803] TeacherClass Member
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
- NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
- Person * person = [[person alloc] init];
- Teacher * teacher = [[teacher alloc] init];
-
- // Yes
- If([Teacher iskindofclass: [teacherClass]) {
- Nslog (@ "teacher is a subclass of teacher Class or teacher ");
- }
- // Yes
- If([Teacher iskindofclass: [PersonClass]) {
- Nslog (@ "teacher is a subclass of the person class or person ");
- }
- // Yes
- If([Teacher iskindofclass: [nsobjectClass]) {
- Nslog (@ "teacher is a subclass of nsobject class or nsobject ");
- }
- [Person release];
- [Teacher release];
- [Pool release];
14:34:17. 315 objectivectest [2595: f803] TeacherYesTeacherClass orTeacherSubclass
14:34:17. 316 objectivectest [2595: f803] TeacherYesPersonClass orPersonSubclass
14:34:17. 316 objectivectest [2595: f803] TeacherYesNsobjectClass orNsobjectSubclass
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.
- // 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
- // 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:
- 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
- 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:
- 14:57:55. 905 objectivectest [3085: f803] instructor Zhang San teaches mathematics
From: http://www.linuxidc.com/Linux/2012-07/64453.htm
My computer verification:
There is a class of words with a display method (words + subof is the category of words)
Respondstoselector is not found in this method, strange, where is the problem?
It turns out that there is an additional colon behind the display, and it will be normal after it is removed. This is different from what is used in the action?