Objective-C reflection instance

Source: Internet
Author: User
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.
  1. # Import <Foundation/Foundation. h>
  2. @ Interface person: nsobject
  3. {
  4. Nsstring * Name;
  5. }
  6. -(Void) Setname :( nsstring *) N;
  7. @ End
  1. # Import "person. H"
  2. @ Implementation person
  3. -(Void) Setname :( nsstring *) N
  4. {
  5. Name = N;
  6. }
  7. @ End
1.2 create a teacher Class
  1. # Import "person. H"
  2. @ Interface Teacher: person
  3. -(Void) Teach;
  4. @ End
  1. # Import "teacher. H"
  2. @ Implementation teacher
  3. -(Void) Teach
  4. {
  5. Nslog (@ "I teach mathematics ");
  6. }
  7. @ End
1.3 first, we will experiment with the ismemberofclass method.
  1. NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
  2. Person * person = [[person alloc] init];
  3. Teacher * teacher = [[teacher alloc] init];
  4. // Yes
  5. If([Teacher ismemberofclass: [teacherClass]) {
  6. Nslog (@ "member of the Teacher class ");
  7. }
  8. // No
  9. If([Teacher ismemberofclass: [PersonClass]) {
  10. Nslog (@ "member of the Teacher Person class ");
  11. }
  12. // No
  13. If([Teacher ismemberofclass: [nsobjectClass]) {
  14. Nslog (@ "member of the teacher nsobject class ");
  15. }
  16. [Person release];
  17. [Teacher release];
  18. [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
  1. NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
  2. Person * person = [[person alloc] init];
  3. Teacher * teacher = [[teacher alloc] init];
  4. // Yes
  5. If([Teacher iskindofclass: [teacherClass]) {
  6. Nslog (@ "teacher is a subclass of teacher Class or teacher ");
  7. }
  8. // Yes
  9. If([Teacher iskindofclass: [PersonClass]) {
  10. Nslog (@ "teacher is a subclass of the person class or person ");
  11. }
  12. // Yes
  13. If([Teacher iskindofclass: [nsobjectClass]) {
  14. Nslog (@ "teacher is a subclass of nsobject class or nsobject ");
  15. }
  16. [Person release];
  17. [Teacher release];
  18. [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.

  1. // Yes
  2. If([Teacher respondstoselector: @ selector (setname:)] = Yes ){
  3. Nslog (@ "teacher responds to setsize: Method ");
  4. }
  5. // No
  6. If([Teacher respondstoselector: @ selector (ABCDE)] = Yes ){
  7. Nslog (@ "teacher responds to nonexistant method ");
  8. }
  9. // Yes
  10. If([Teacher respondstoselector: @ selector (alloc)] = Yes ){
  11. Nslog (@ "teacher Class responds to alloc Method \ n ");
  12. }

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
  1. // No
  2. If([Person instancesrespondtoselector: @ selector (teach)] = Yes ){
  3. Nslog (@ "person instance responds to teach method ");
  4. }
  5. // Yes
  6. If([Teacher instancesrespondtoselector: @ selector (teach)] = Yes ){
  7. Nslog (@ "teacher instance responds to teach method ");
  8. }
  9. // Yes
  10. If([Teacher instancesrespondtoselector: @ selector (setname :)] = Yes ){
  11. Nslog (@ "teacher instance responds to setname: Method ");
  12. }

Print result:

  1. 14:52:29. 378 objectivectest [2961: f803] Teacher instance responds to teach Method
  2. 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

  1. NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
  2. Person * person = [[person alloc] init];
  3. Teacher * teacher = [[teacher alloc] init];
  4. Id P = person;
  5. ID t = teacher;
  6. [T setname: @ "Miss Zhang"];
  7. [T teach];
  8. [Person release];
  9. [Teacher release];
  10. [Pool release];

Print result:

  1. 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?

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.