Learn Objective-C, Objects (Part 7): Introspection

Introspection is that ability to ask questions about a class and find out more of its properties. This is a fundamental concept of a group of concepts known as polymorphism, dynamic typing, and dynamic binding, which form a central tenet of object-oriented programming. Before we begin, there is another part of the language that we have to cover.

Selector (@selector())

Remember how methods are also known as selectors (because a method is selected at runtime)? Turns out, you can select a method yourself, and pass it as an argument to a method. The syntax is simple:


Remember that a method name includes its colons, such as setNumerator:overDenominator: (if appropriate).

You can declare a selector as a variable to be used in multiple places with the following syntax:

SEL action = @selector(doSomethingWithObject:)

Note that a selector is of type SEL.

Introspection Methods

Here is a list of most of the introspection methods. A few deal with some more advanced topics, such as protocols; they will be introduced in context. Note that the classType argument refers to an object’s class, which is returned by invoking the class method, as in [Fraction class];.

  • -(BOOL)isKindOfClass:classType Is the object a member of that class or a subclass?
  • -(BOOL)isMemberOfClass:classType Is the object a member of that class? *-(BOOL)respondsToSelector:aSelector Does an object (class or instance) respond to the selector’s method? *+(BOOL)instancesRespondToSelector:aSelector Do instances of the class respond to the selector’s method? *+(BOOL)isSubclassOfClass:classType Is this class a subclass of the specified class? *-(id)performSelector:aSelector Ask the class to perform the method specified by the selector, returning whatever the method is intended to return. *-(id)performSelector:aSelector withObject:someObject Performs the selector, passing in someObject as the first (and hopefully only) argument, returning whatever the method is designed to return. *-(id)performSelector:aSelector withObject:someObject withObject:anotherObject Perform the selector, passing in the two arguments in that order to the (assumed) two parameters defined by the selector, returning whatever the method is designed to return.

Working off our Fraction and MixedNumber classes, we can have the following routine:

  1. int main (int argc, const char * argv[]) {
  2.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    Fraction *frac = [[Fraction alloc] init];
  3.     MixedNumber *mn = [[MixedNumber alloc] init];
  5.     if ([frac isMemberOfClass:[Fraction class]])
  6.         NSLog(@"frac is member of Fraction class");
  7.     if ([mn isMemberOfClass:[Fraction class]])
  8.         NSLog(@"mn is member of Fraction class");
  9.     if ([mn isMemberOfClass:[NSObject class]])      // Remember that everything
  10.         NSLog(@"mn is member of NSObject class"); // inherits from NSObject
  12.     if ([frac isKindOfClass:[Fraction class]])
  13.         NSLog(@"frac is kind of Fraction class");
  14.     if ([mn isKindOfClass:[Fraction class]])
  15.         NSLog(@"mn is kind of Fraction class");
  16.     if ([frac isKindOfClass:[NSObject class]])
  17.         NSLog(@"frac is kind of NSObject class");
  19.     if ([frac respondsToSelector:@selector(add:)])
  20.         NSLog(@"frac responds to add:");
  21.     if ([mn respondsToSelector:@selector(add:)])
  22.         NSLog(@"mn responds to add:");
  23.     if ([Fraction respondsToSelector:@selector(alloc)])
  24.         NSLog(@"Fraction class responds to alloc");
  26.     if ([Fraction instancesRespondToSelector:@selector(subtract:)])
  27.         NSLog(@"Instances of Fraction respond to subtract:");
  29.     if ([MixedNumber isSubclassOfClass:[Fraction class]])
  30.         NSLog(@"MixedNumber is a subclass of Fraction");
  32.     [frac release];
  33.     [mn release];
  34.     [pool drain];
  35.     return 0;
  36. }

The output is

  1. frac is member of Fraction class
  2. frac is kind of Fraction class
  3. mn is kind of Fraction class
  4. frac is kind of NSObject class
  5. frac responds to add:
  6. mn responds to add:
  7. Fraction class responds to alloc
  8. Instances of Fraction respond to subtract:
  9. MixedNumber is a subclass of Fraction

Introspection really isn’t a difficult topic—the only potential source of error stems from confusing the functionality of isKindOfClass: and isMemberOfClass:. Somehow, just remember that isKindOfClass: involves descendants (subclasses), while isMemberOfClass: does not.

This post is part of the Learn Objective-C in 24 Days course.

Author: Feifan Zhou